如何_directly_从.JAR - JAVA中的子目录中获取文件列表 [英] How to _directly_ get a list of files from a sub-directory in a .JAR - JAVA

查看:221
本文介绍了如何_directly_从.JAR - JAVA中的子目录中获取文件列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法直接获取.jar中指定子目录中所有文件的列表。

I'm looking for a way to directly get a list of all files within a specified sub-directory within a .jar.

这类似:如何列出JAR文件中的文件?

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

But unfortunately the method listed above will iterate over all files in the entire .jar. When working with a .jar with many files in it - this seems impractical as is my case. There has to be a way to directly path to a sub directory within the .jar and iterate over its contents, without having to iterate over all contents of the jar and then filter the results to find those entries that you care about.

这里大致是我所拥有的:

Here is roughly what I have:

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;
}

变量: url - >:jar:file:/ C:/jboss-4.2.2.GA/server/username/tmp/deploy/tmp8421264930467110704SomeEar.ear-contents/SomeBusiness.jar!/ subdir1 / subdir2 / subdir3 / subdir4 /

The variable: url ->: jar:file:/C:/jboss-4.2.2.GA/server/username/tmp/deploy/tmp8421264930467110704SomeEar.ear-contents/SomeBusiness.jar!/subdir1/subdir2/subdir3/subdir4/

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

The path: /subdir1/subdir2/subdir3/subdir4/ is exactly where I want to iterate.

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

Debugging reveals that juc is indeed created correctly and pointing to the correct path. jarFile then as expected, just gives me the jar file path which is why I lose the subdirectories and why I start iterating at the root. This all makes sense. This clearly isn't the correct way. There has to be another way!

使用理论上指向我感兴趣的正确目录的 JarURLConnection 搞乱,不会透露什么有用的。有 JarURLConnection.getInputStream()。调试器指示这最终包含 ZipFileInputStream ,但是我无法访问它,并且进一步看起来它只是 ZipFileInputStream ZipFile - 这让我回到了第一个方位。

Messing around with the JarURLConnection which theoretically points to the correct directory I am interested in, doesn't reveal anything useful. There is JarURLConnection.getInputStream(). Debugger indicates that this ultimately holds a ZipFileInputStream, but I can't get access to it, and looking further it looks like its just the ZipFileInputStream to the ZipFile - which puts me back at square one.

推荐答案

抱歉,没有别的办法,至少没有办法标准的java库。一个zip文件只包含一个条目列表,其中没有随机访问查找。可能还有其他库可以为您解析列表并创建某种分层的条目映射,但不管怎样,某些代码需要遍历所有条目以找到所需内容。

sorry, there isn't another way, at least not with the standard java libraries. a zip file just contains a list of entries, there is no "random access" lookup in it. there may be other libraries out there that parse through the list for you and create some sort of hierarchical map of entries, but either way, some code needs to iterate through all the entries to find what you need.

这篇关于如何_directly_从.JAR - JAVA中的子目录中获取文件列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆