简体   繁体   English

从JarEntry获取JarFile

[英]Get a JarFile from a JarEntry

My current task is to iterate a .jar to see if it has .jars inside. 我当前的任务是迭代.jar以查看其内部是否包含.jars。 Then I have to get those .jars and check certain file in each one.To do that, I am using JarFile to get the JarEntries. 然后我必须获取这些.jar文件并检查每个文件中的特定文件。为此,我正在使用JarFile获取JarEntries。 The problem is, How do I do to get again the JarFile from the current Jar Entry?. 问题是,如何从当前的Jar条目中再次获取JarFile? Is it possible? 可能吗?

Jar:
 Jar1:   
   /META-INF/source/file1.txt
 Jar2: 
   /META-INF/source/file1.txt

Here is my current code: 这是我当前的代码:

 public static void main(String[] args) throws IOException,   URISyntaxException {

    String dir = Paths.get(".").toAbsolutePath().normalize().toString() + "/target";
    Collection<String> jarBuildList = JarFinder.getJars(dir);
    for(String buildJar : jarBuildList){

        File file = new File(buildJar);
        JarFile buildJarFile = new JarFile(file);

             Enumeration<JarEntry> entries = buildJarFile.entries();
             while (entries.hasMoreElements()) {
                JarEntry jarEntry = entries.nextElement();
                if (jarEntry.getName().contains(".jar")){
                      //Having the JarEntry, enter to that jar javaEntry and get a file1.txt
                }                       
             }              
        }

    }
}

How do I do to get again the JarFile from the current Jar Entry?. 如何从当前的Jar条目中再次获取JarFile? Is it possible? 可能吗?

Not directly, no. 不直接,不。 Your alternatives are: 您的替代方法是:

  • copy the content of the entry to a file in the file system and use JarFile to open that, or 将条目的内容复制到文件系统中的文件,然后使用JarFile打开它,或者

  • use the JarInputStream API. 使用JarInputStream API。

FWIW, the reason that JarFile only works on files in the file system is that its implementation is a native library which uses native code to open and read the source file. FWIW, JarFile仅对文件系统中的文件起作用的原因是,其实现是一个本机库,该库使用本机代码打开和读取源文件。 It was done this way (I think) because loading from JAR / ZIP files has to work before the JVM is fully bootstrapped. 这种方式(我认为)是因为从JAR / ZIP文件加载必须在JVM完全引导之前完成。 Then the API became frozen ... for the normal reasons. 然后,出于正常原因,API被冻结了。

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

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