开发者

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

开发者 https://www.devze.com 2022-12-30 05:14 出处:网络
ZipeFile file = new ZipFile(filename); ZipEntry folder = this.file.getEntry(\"some/path/in/zip/\"); if (folder == null || !folder.isDirectory())
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开发者_StackOverflow中文版 zipped folder?


It doesn't look like there's a way to enumerate ZipEntry under a certain directory.

You'd have to go through all ZipFile.entries() and filter the ones you want based on the ZipEntry.getName() and see if it 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);
    }
}


You don't - at least, not directly. ZIP files are not actually hierarchical. Enumerate all the entries (via ZipFile.entries() or ZipInputStream.getNextEntry()) and determine which are within the folder you want by examining the name.


Can you just use entries()? API link

0

精彩评论

暂无评论...
验证码 换一张
取 消