Java - How to decompress files from a zip file

Posted on November 14, 2016


Package java.util.zip provides classes for reading and writing zip and gzip file formats.

In this post, we will learn how to decompress a zip file using java.util.zip.ZipInputStream and java.util.zip.ZipFile.

ZipInputStream - This class is used to read zip entries in sequential order.

ZipFile – This class internally uses a random-access file to read the zip entries.

Decompress a zip file using java.util.zip.ZipInputStream

Here is an example of to demonstrate the usages of ZipInputStream.

UnzipFileExample1.java

package com.boraji.tutorial.io;

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

/**
 * @author imssbora
 */
public class UnzipFileExample1 {
  public static void main(String[] args) {
    File zipFile = new File("Test.zip");
    File output = new File("Unzip");
    FileInputStream fileInputStream = null;
    ZipInputStream zipInputStream = null;
    try {
      fileInputStream = new FileInputStream(zipFile);
      zipInputStream = new ZipInputStream(fileInputStream);

      // Get ZipEntry
      ZipEntry entry = zipInputStream.getNextEntry();
      while (entry != null) {
        if (entry.isDirectory()) {

          // Create directory
          File dir = new File(output, entry.getName());
          if (!dir.exists()) {
            dir.mkdirs();
          }
        } else {

          // Read zipEntry and write to a file.
          File file = new File(output, entry.getName());
          FileOutputStream fileOutputStream = new FileOutputStream(file);
          int i;
          byte[] data = new byte[1024];
          while ((i = zipInputStream.read(data)) != -1) {
            fileOutputStream.write(data, 0, i);
          }

          fileOutputStream.close();

        }
        // Get next entry
        entry = zipInputStream.getNextEntry();
      }

    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        if (fileInputStream != null) {
          fileInputStream.close();
        }
        if (zipInputStream != null) {
          zipInputStream.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}

 

Decompress a zip file using java.util.zip.ZipFile

The following example demonstrate the usages of ZipFile.

UnzipFileExample2.java

package com.boraji.tutorial.io;

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

/**
 * @author imssbora
 */
public class UnzipFileExample2 {
  public static void main(String[] args) {
    File output = new File("Unzip2");
    ZipFile zipFile = null;
    try {

      zipFile = new ZipFile(new File("Test.zip"));

      Enumeration<? extends ZipEntry> entries = zipFile.entries();

      while (entries.hasMoreElements()) {
        ZipEntry entry = (ZipEntry) entries.nextElement();
        if (entry.isDirectory()) {

          // Create directory
          File dir = new File(output, entry.getName());
          if (!dir.exists()) {
            dir.mkdirs();
          }
        } else {

          // Get zipEntry name and create an output stream.
          File file = new File(output, entry.getName());
          FileOutputStream fileOutputStream = new FileOutputStream(file);
          InputStream inputStream = zipFile.getInputStream(entry);

          // Read Zip file entry
          int i;
          byte[] data = new byte[1024];
          while ((i = inputStream.read(data)) != -1) {
            fileOutputStream.write(data, 0, i);
          }

          fileOutputStream.close();
          inputStream.close();
        }
      }
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        if (zipFile != null) {
          zipFile.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}