Java - ZIP File Reading

Introduction

You can read the data from the ZipInputStream object for the current zip entry.

The following code illustrates how to read contents of a ZIP file.

Demo

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class Main {
  public static void main(String[] args) {
    String zipFileName = "ziptest.zip";
    String unzipdirectory = "extracted";
    unzip(zipFileName, unzipdirectory);/* ww  w . j  a  v  a  2s.  c  o  m*/
  }

  public static void unzip(String zipFileName, String unzipdir) {
    try (ZipInputStream zis = new ZipInputStream(new BufferedInputStream(
        new FileInputStream(zipFileName)))) {

      // Read each entry from the ZIP file
      ZipEntry entry = null;
      while ((entry = zis.getNextEntry()) != null) {
        // Extract teh entry's contents
        extractEntryContent(zis, entry, unzipdir);
      }

      System.out.println("ZIP file's contents have been extracted to "
          + (new File(unzipdir)).getAbsolutePath());
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  public static void extractEntryContent(ZipInputStream zis, ZipEntry entry,
      String unzipdir) throws IOException, FileNotFoundException {

    String entryFileName = entry.getName();
    String entryPath = unzipdir + File.separator + entryFileName;

    // Create the entry file by creating necessary directories
    createFile(entryPath);

    // Create an output stream to extract the content of the
    // zip entry and write to the new file
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(
        entryPath));

    byte[] buffer = new byte[1024];
    int count = -1;
    while ((count = zis.read(buffer)) != -1) {
      bos.write(buffer, 0, count);
    }

    bos.close();
  }

  public static void createFile(String filePath) throws IOException {
    File file = new File(filePath);
    File parent = file.getParentFile();

    // Create all parent directories if they do not exist
    if (!parent.exists()) {
      parent.mkdirs();
    }
    file.createNewFile();
  }
}

Result

Related Topics