繁体   English   中英

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

[英]How do I enumerate the content of a zipped folder in Java?

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?

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

您必须遍历所有ZipFile.entries()并根据ZipEntry.getName()过滤所需的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);
    }
}

你没有 - 至少,不是直接的。 ZIP文件实际上不是分层的。 枚举所有条目(通过ZipFile.entries()或ZipInputStream.getNextEntry())并通过检查名称确定所需的文件夹。

你能用到entries()吗? API链接

暂无
暂无

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

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