如何从jar中读取文件对象

时间:2014-05-27 13:43:34

标签: java jar java-io imageicon zipfile

我有一个包含文件夹结构的jar文件,即......

罐/ COM /富/酒吧/图像

在Images目录中我有几张图片。我试图用getResourceAsStream()读出它们,并试图通过ZipEntries来获取它们。

我的所有尝试都没有结果,如果有人可以请举例说明如何做到这一点。

P.S不要担心关闭资源和错误处理等。我将把这部分放进去。

感谢您的帮助。

这是我试过的zipfile条目代码

private File[] getFilesFromPath(String location) {
        File[] files = null;
        try {
            CodeSource src = this.getClass().getProtectionDomain().getCodeSource();
            if (src != null) {
                URL jar = src.getLocation();
                ZipInputStream zip = new ZipInputStream(jar.openStream());
                while(true) {
                    ZipEntry e = zip.getNextEntry();
                    if (e == null)
                        break;
                    String name = e.getName();
                    if (name.endsWith(location)) {
                         files = new File(name).listFiles((new FileFilter() {
                            @Override
                            public boolean accept(File f) {
                                return f.isFile();
                            }
                        }));
                         for (File file : files){
                             System.out.println(file.getName());
                         }
                    }
                }
            }
        }catch (IOException e) {
            e.printStackTrace();
        }
        return files;
    }

private File[] getFilesFromPath(String location) { File[] files = null; try { CodeSource src = this.getClass().getProtectionDomain().getCodeSource(); if (src != null) { URL jar = src.getLocation(); ZipInputStream zip = new ZipInputStream(jar.openStream()); while(true) { ZipEntry e = zip.getNextEntry(); if (e == null) break; String name = e.getName(); if (name.endsWith(location)) { files = new File(name).listFiles((new FileFilter() { @Override public boolean accept(File f) { return f.isFile(); } })); for (File file : files){ System.out.println(file.getName()); } } } } }catch (IOException e) { e.printStackTrace(); } return files; }

2 个答案:

答案 0 :(得分:1)

以下代码应该适用于您的情况,因为我已经使用以下代码从现有jar中读取hbm文件以创建动态会话工厂。它只是读取jar文件,然后查看给定位置的图像文件夹(com / foo / bar / images),然后它假定该文件夹中的所有文件都是图像文件。它将文件转换为输入流。

将JARCLASSNAME从jar文件更改为您的类名。

CodeSource src = JARCLASSNAME.class.getProtectionDomain().getCodeSource();
            if (src != null) {
              URL jar = src.getLocation();
              ZipInputStream zip = new ZipInputStream(jar.openStream());
              while(true) {
                ZipEntry e = zip.getNextEntry();
                if (e == null)
                  break;
                String name = e.getName();
                // Check the folder name if folder name match then only read the file
                if (name.startsWith("com/foo/bar/images")) {

                    InputStream inputStream =JARCLASSNAME.class.getClassLoader().getResourceAsStream(name);


              }
            } 
            }

答案 1 :(得分:0)

构建网址图片怎么样?

URL imageURL = JARFILENAME.class.getResource.("pic1.gif");

然后制作如下图像:

Image yourImage = Toolkit.getDefaultToolKit().getImage(imageURL);

网址:http://www.daniweb.com/software-development/java/threads/21906/accessing-images-in-jar-file

相关问题