ripper234
ripper234

Reputation: 230008

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?

Upvotes: 3

Views: 1868

Answers (3)

polygenelubricants
polygenelubricants

Reputation: 383726

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

Upvotes: 5

developmentalinsanity
developmentalinsanity

Reputation: 6229

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.

Upvotes: 1

Pops
Pops

Reputation: 30828

Can you just use entries()? API link

Upvotes: 0

Related Questions