如何在Java中枚举压缩文件夹的内容?

rip*_*234 3 java zip

ZipeFile file = new ZipFile(filename);
ZipEntry folder = this.file.getEntry("some/path/in/zip/");
if (folder == null || !folder.isDirectory())
  throw new Exception();

// now, how do I enumerate the contents of the zipped folder?
Run Code Online (Sandbox Code Playgroud)

pol*_*nts 5

它看起来不像是ZipEntry在某个目录下枚举的方法.

你必须经历所有ZipFile.entries()并根据ZipEntry.getName()它过滤你想要的并查看它String.startsWith(String prefix).

String specificPath = "some/path/in/zip/";

ZipFile zipFile = new ZipFile(file);
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
    ZipEntry ze = entries.nextElement();
    if (ze.getName().startsWith(specificPath)) {
        System.out.println(ze);
    }
}
Run Code Online (Sandbox Code Playgroud)