Unzip or decompress files in java using ZipInputStream & ZipEntry (example)

By | December 14, 2016
  • Given a zipped or compressed file in java.
  • Write a program to unzip or decompress the contents of zip file using ZipInputStream in java.
    • Zip file may contain nested directory structure.
    • e.g. Zip file may have compressed files having following directory structure.
      • FirstFile.txt
      • SecondFile.txt
      • Dir1\Dir1_F1.txt
      • Dir1\Dir1_F2.txt
      • Dir1\Dir11\Dir11_F1.txt
      • Dir1\Dir11\Dir11_F2.txt …..
  • Read the content of zip files using ZipInputStream in java.
    • Each entity in a zip file corresponds to ZipEntry.

Program flow: Unzip files in java using ZipInputStream & ZipEntry

  1. Read the content of a Zip file using ZipInputStream in java.
    • Each entity in a zip file corresponds to ZipEntry.
  2. Create an output file corresponding to each ZipEntry.
  3. Write content to output file.
  4. Perform 1 to 3 steps for each entity in a Zip file.

Program: Unzip or decompress files in java (ZipInputStream & ZipEntry)

package org.learn;

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;

public class UnFilesInDirectories {

    public static void main(String[] args) throws IOException {
        File inputDirectory = new File("C:\\contents\\zipDirectory.zip");
        File outputUnZippedContents = new File("C:\\output\\unzipContents");
        outputUnZippedContents.delete();
        outputUnZippedContents.getParentFile().mkdirs();
        unZip(inputDirectory, outputUnZippedContents);
    }

    private static void unZip(File zipFile, File outputDirectory) throws IOException {

        try (ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipFile))) {
            int numberOfFiles = 0;
            ZipEntry zipEntry;

            while ((zipEntry = zipInputStream.getNextEntry()) != null) {
                numberOfFiles++;
                System.out.printf("%d. Extracting content:%s\n",numberOfFiles, zipEntry.getName());
                File unZippedFile = new File(outputDirectory + File.separator + zipEntry.getName());

                //Create output director
                unZippedFile.getParentFile().mkdirs();
                System.out.printf("Creating new file :%s\n", unZippedFile.getCanonicalPath());

                //Write contents to file
                writeContents(zipInputStream, unZippedFile);
                System.out.printf("Written content to file:%s\n\n",unZippedFile.getCanonicalPath());

                //Close current entry
                zipInputStream.closeEntry();
            }
            System.out.printf("Finished execution, UnZipped file count:%d", numberOfFiles);
        }
    }

    private static void writeContents(ZipInputStream zipInputStream, File outputFile)
                                                                        throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
        int len;
        byte[] content = new byte[1024];
        while ((len = zipInputStream.read(content)) > 0) {
            fileOutputStream.write(content, 0, len);
        }
        fileOutputStream.close();
    }
}

Output: Unzip or decompress files in java using ZipInputStream & ZipEntry

 1. Extracting content:Dir1\Dir11\Dir11_F1.txt
Creating new file :C:\output\unzipContents\Dir1\Dir11\Dir11_F1.txt
Written content to file:C:\output\unzipContents\Dir1\Dir11\Dir11_F1.txt

2. Extracting content:Dir1\Dir11\Dir11_F2.txt
Creating new file :C:\output\unzipContents\Dir1\Dir11\Dir11_F2.txt
Written content to file:C:\output\unzipContents\Dir1\Dir11\Dir11_F2.txt

3. Extracting content:Dir1\Dir1_F1.txt
Creating new file :C:\output\unzipContents\Dir1\Dir1_F1.txt
Written content to file:C:\output\unzipContents\Dir1\Dir1_F1.txt

4. Extracting content:Dir1\Dir1_F2.txt
Creating new file :C:\output\unzipContents\Dir1\Dir1_F2.txt
Written content to file:C:\output\unzipContents\Dir1\Dir1_F2.txt

5. Extracting content:Dir2\Dir21\Dir21_F1.txt
Creating new file :C:\output\unzipContents\Dir2\Dir21\Dir21_F1.txt
Written content to file:C:\output\unzipContents\Dir2\Dir21\Dir21_F1.txt

6. Extracting content:Dir2\Dir21\Dir21_F2.txt
Creating new file :C:\output\unzipContents\Dir2\Dir21\Dir21_F2.txt
Written content to file:C:\output\unzipContents\Dir2\Dir21\Dir21_F2.txt

7. Extracting content:Dir2\Dir2_F1.txt
Creating new file :C:\output\unzipContents\Dir2\Dir2_F1.txt
Written content to file:C:\output\unzipContents\Dir2\Dir2_F1.txt

8. Extracting content:Dir2\Dir2_F2.txt
Creating new file :C:\output\unzipContents\Dir2\Dir2_F2.txt
Written content to file:C:\output\unzipContents\Dir2\Dir2_F2.txt

9. Extracting content:FirstFile.txt
Creating new file :C:\output\unzipContents\FirstFile.txt
Written content to file:C:\output\unzipContents\FirstFile.txt

10. Extracting content:SecondFile.txt
Creating new file :C:\output\unzipContents\SecondFile.txt
Written content to file:C:\output\unzipContents\SecondFile.txt

11. Extracting content:ThirdFile.txt
Creating new file :C:\output\unzipContents\ThirdFile.txt
Written content to file:C:\output\unzipContents\ThirdFile.txt

Finished execution, UnZipped file count:11