简体   繁体   English

给定特定路径读取jar文件的内容

[英]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. 我有一个名为“ san.jar”的jar文件,其中包含“ classes”,“ resources”等各种文件夹。例如,我有一个文件夹结构,例如“ resources / assets / images”,在该文件夹下有各种图像没有相关的任何信息,例如图像名称或文件夹下的图像数量,因为jar文件是私有文件,因此我无法解压缩jar文件。

OBJECTIVE: I need to get all the files under the given path without iterating over the whole jar file. 目的:我需要获取给定路径下的所有文件,而无需遍历整个jar文件。

Right now what I am doing is iterating through each and every entry and whenever i come across .jpg file, I perform some operation. 现在,我正在做的事情是遍历每个条目,每当我遇到.jpg文件时,我都会执行一些操作。 Here for reading just the "resources/assets/images", I am iterating through the whole jarfile. 在这里,仅阅读“资源/资产/图像”,我将遍历整个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. 现在,我正在做的事情是遍历每个条目,每当我遇到.jpg文件时,我都会执行一些操作。 Here for reading just the "resources/assets/images", I am iterating through the whole jarfile. 在这里,仅阅读“资源/资产/图像”,我将遍历整个jarfile。

With Java 8 and filesystems it is pretty easy now, 有了Java 8和文件系统,现在非常容易,

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. Files.find只会搜索所需深度的提供的路径。

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. 使用正则表达式可以更简洁地完成此操作。当jpg文件具有大写扩展名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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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