How to list the contents 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?
+3
source share
3 answers

There doesn't seem to be a way to list ZipEntryin a specific directory.

You need to go through ZipFile.entries()and filter out the ones you want based on ZipEntry.getName()and look, not 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);
    }
}
+5
source

You do not - at least not directly. ZIP files are not really hierarchical. List all the entries (via ZipFile.entries () or ZipInputStream.getNextEntry ()) and determine which ones are in the folder you want by examining the name.

+1
source

entries()? API

0

Source: https://habr.com/ru/post/1745116/


All Articles