简体   繁体   中英

Read contents of jar file given specific path

I have a jar file named "san.jar" with various folders like "classes", "resources", etc., Say for eg 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.

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.

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();

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM