如何_directly_从.JAR子目录中获取文件列表 - JAVA

问题描述:

我正在寻找一种方式直接到获取.jar中指定子目录内的所有文件的列表。如何_directly_从.JAR子目录中获取文件列表 - JAVA

这是类似的:How do I list the files inside a JAR file?

但不幸的是上面列出的方法会遍历所有的文件,在整个的.jar。使用带有许多文件的.jar时 - 这与我的情况似乎不切实际。必须有一种方法可以直接路径到.jar中的子目录并迭代其内容,而不必遍历jar的所有内容,然后过滤结果以找到您关心的条目。

这大致就是我:

public static ArrayList<String> loadInternalFileListing(MetaDataType type, MetaDataLocation location, String siteId) 
{ 
    ArrayList<String> filenameList = new ArrayList<String>(); 
    URL url = getInternalFile(type, null, location, siteId); 

    JarURLConnection juc = null; 
    JarFile jarFile = null; 

    try 
    { 
    juc = (JarURLConnection) url.openConnection(); 
    jarFile = juc.getJarFile(); 
    Enumeration<JarEntry> entries = jarFile.entries(); 

    for(JarEntry jarEntry = entries.nextElement(); entries.hasMoreElements(); jarEntry = entries.nextElement()) 
    { 
     ...//logic here 
    } 
    } 
    ... //catch, handle exceptions, finally block and other logic 

    return filenameList; 
} 

变量:网址 - >中:jar:文件:/ C:/jboss-4.2.2.GA/server/username/tmp /deploy/tmp8421264930467110704SomeEar.ear-contents/SomeBusiness.jar!/subdir1/subdir2/subdir3/subdir4/

的路径:/ subdir1/subdir2/subdir3/subdir4 /正是我想要的迭代。

调试显示juc确实正确创建并指向正确的路径。 jar文件然后如预期,只是给了我的jar文件路径,这就是为什么我失去了子目录,为什么我开始迭代在根。这一切都有道理。这显然不是正确的方法。必须有另一种方式!

乱收费JarURLConnection理论上指向我感兴趣的正确目录,没有显示任何有用的东西。有JarURLConnection.getInputStream()。调试器表明这最终持有ZipFileInputStream,但我无法访问它,并进一步看起来像它只是ZipFileInputStreamZipFile - 这使我回到了第一。

对不起,没有别的办法,至少不用标准的java库。一个zip文件只包含一个条目列表,没有“随机访问”查找。那里可能有其他的图书馆为你解析列表并创建某种分层图的条目,但无论哪种方式,一些代码需要迭代所有条目以找到你需要的东西。

+0

发布后,我认为这可能是由于zip文件的格式/结构造成的。谢谢。 –