Extracting the Contents of a Zip File in Java

The following short program shows how to extract all of the files that are inside a Zip file. Java provides us with a number of classes to work with zip files, and their use is pretty straightforward. To extract the files to their original directories, I also had to create the directory in the file system.

/*******************************************************************************
 * Copyright (c) 2012 Arieh 'Vainolo' Bibliowicz
 * You can use this code for educational purposes. For any other uses
 * please contact me: vainolo@gmail.com
 *******************************************************************************/
package com.vainolo.examples.file;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * Extract a zip file to a folder. No error checking or argument validation is done.
 * 
 * @author vainolo
 */
public class ZipExtracter {
  public static void openZipFile(String zipFilename, String destinationDirname) throws IOException {
    byte[] buffer = new byte[1024];
    int bytesRead = 0;
    File zipFile = new File(zipFilename);
    File destinationDir = new File(destinationDirname);
    ZipFile zip = new ZipFile(zipFile);
    Enumeration<? extends ZipEntry> zipEntries = zip.entries();
    while (zipEntries.hasMoreElements()) {
      ZipEntry entry = zipEntries.nextElement();
      if (entry.isDirectory()) {
        File newDir = new File(destinationDir, entry.getName());
        newDir.mkdirs();
      } else {
        BufferedInputStream inputStream = new BufferedInputStream(zip.getInputStream(entry));
        File outputFile = new File(destinationDir, entry.getName());
        BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));
        while ((bytesRead = inputStream.read(buffer)) != -1) {
          outputStream.write(buffer, 0, bytesRead);
        }
        inputStream.close();
        outputStream.close();
      }
    }
  }
}

Some thoughts disclaimers on my implementation:

First, I do no parameter validation or error checking. So what can happen? here are a few examples:

  • The input file may not exist.
  • The input file exists but is not a zip file.
  • The output directory does not exist.
  • Problems may occur in the middle of the process of reading from the input file or writing to the file system (for example if your output file system is a network disk and your cat just ate the network cable).

As you can see, many things can go wrong (and will), so a GOOD implementation (not a simple example) should check for these problems and deal with them as best as it can. I personally don’t like the way Java forces you to catch all exceptions, because it makes code harder to read. But in real systems you cannot have this luxury.

Second, the implementation assumes that the Enumeration returned by the ZipFile is ordered – directories before the files in the directories. I didn’t find any comments about this in the documentation of ZipFile… maybe it’s part of the zip standard.

Enhanced by Zemanta

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.