Home  Java compression intro  Deflater how-to  Deflater algorithm  Deflater configuration  Text compression performance  GZIP files  ZIP files

Search this site:
Threads Database Profiling Regular expressions Random numbers Compression Exceptions C Equivalents in Java

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 {

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();
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.


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

  • Note that the path separator in ZIP files is a slash (/), even on other platforms such as Windows, where the normal path separator is something different such as a backslash.
comments powered by Disqus

Written by Neil Coffey. Copyright © Javamex UK 2012. All rights reserved.