Reading a ZIP file in Java: enumeration and metadata

We so far considered the case of reading a single zip file entry whose name we knew. An alternative is to enumerate the entries in the zip file and then decide which entry or entries to read from based on the list given to us. To do so, we call the ZipFile.entries() method. This returns us an Enumeration of ZipEntry objects which we can then query for their name or use as a reference to get an input stream from the ZipFile. For example, the following pattern will read from just the entries whose name ends in .txt:

ZipFile zf = new ZipFile(f);
try {
  for (Enumeration<? extends ZipEntry> e = zf.entries();
        e.hasMoreElements();) {
    ZipEntry ze = e.nextElement();
    String name = ze.getName();
    if (name.endsWith(".txt")) {
      InputStream in = zf.getInputStream(ze);
      // read from 'in'
    }
  }
} finally {
  zf.close();
}

Querying metadata

ZIP files store a handful of metadata about files in the archive: a timestamp, compressed/uncompressed size (from which the compression ratio can be calculated), an optional comment field plus an optional 'arbitrary data' field. Various methods on the ZipEntry object allow these fields to be queried.

The creation time of a file is stored as the number of milliseconds since the epoch (midnight, 1 Jan 1970)– i.e. the format required by the Calendar.setTimeInMillis() method. So to get the time as a human-readable string, you can do something such as the following:

Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(ze.getTime());
String dateStr = String.format("%tc", cal);

(See the Formatter class documentation for alternatives to %tc for formatting the date and time.) Note that this assumes that the time in the zip file is in the current locale.

Directories

ZIP files don't support directories per se. The directory structure is implied by giving every entry a full path name.


If you enjoy this Java programming article, please share with friends and colleagues. Follow the author on Twitter for the latest news and rants.

Editorial page content written by Neil Coffey. Copyright © Javamex UK 2021. All rights reserved.