Read contents of jar file given specific path

北慕城南 提交于 2019-12-08 13:56:02

问题


I have a jar file named "san.jar" with various folders like "classes", "resources", etc., Say for e.g i have a folder structure like "resources/assets/images" under which there are various images which I do not have any information about them like name of the images or number of images under the folder as the jar file is private and I am not allowed to unzip the jar.

OBJECTIVE: I need to get all the files under the given path without iterating over the whole jar file.

Right now what I am doing is iterating through each and every entry and whenever i come across .jpg file, I perform some operation. Here for reading just the "resources/assets/images", I am iterating through the whole jarfile.

JarFile jarFile = new JarFile("san.jar");  
for(Enumeration em = jarFile.entries(); em.hasMoreElements();) {  
                String s= em.nextElement().toString();  
                if(s.contains("jpg")){  
                   //do something  
                }  
 }  

Right now what I am doing is iterating through each and every entry and whenever i come across .jpg file, I perform some operation. Here for reading just the "resources/assets/images", I am iterating through the whole jarfile.


回答1:


This code works your purpose

JarFile jarFile = new JarFile("my.jar");

    for(Enumeration<JarEntry> em = jarFile.entries(); em.hasMoreElements();) {  
        String s= em.nextElement().toString();

        if(s.startsWith(("path/to/images/directory/"))){
            ZipEntry entry = jarFile.getEntry(s);

            String fileName = s.substring(s.lastIndexOf("/")+1, s.length());
            if(fileName.endsWith(".jpg")){
                InputStream inStream= jarFile.getInputStream(entry);
                OutputStream out = new FileOutputStream(fileName);
                int c;
                while ((c = inStream.read()) != -1){
                    out.write(c);
                }
                inStream.close();
                out.close();
                System.out.println(2);
            }
        }
    }  
    jarFile.close();



回答2:


This can be done much more concisely with a regex... It will also work when jpg files have upper case extension JPG.

JarFile jarFile = new JarFile("my.jar");

Pattern pattern = Pattern.compile("resources/assets/images/([^/]+)\\.jpg",
        Pattern.CASE_INSENSITIVE);

for (Enumeration<JarEntry> em = jarFile.entries(); em
        .hasMoreElements();) {
    JarEntry entry = em.nextElement();

    if (pattern.matcher(entry.getName()).find()) {
        BufferedImage image = ImageIO.read(jarFile
                .getInputStream(entry));
        System.out.println(image.getWidth() + " "
                + image.getHeight());

    }
}
jarFile.close();



回答3:


With Java 8 and filesystems it is pretty easy now,

Path myjar;
try (FileSystem jarfs = FileSystems.newFileSystem(myjar, null)) {
   Files.find(jarfs.getPath("resources", "assets", "images"), 
              1, 
              (path, attr) -> path.endsWith(".jpg"),
              FileVisitOption.FOLLOW_LINKS).forEach(path -> {
            //do something with the image.
    });
}

Files.find will only search the provided path up the the desired depth.



来源:https://stackoverflow.com/questions/9630268/read-contents-of-jar-file-given-specific-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!