Thursday, 15 June 2017

How to unzip files from zip file by java?

In Java java.util.zip package provide below classes and api’s to convert files to zip file.

ZipInputStream: This class implements an input stream filter for reading files in the ZIP file format.

ZipEntry: Class is used for zip entry.

In below example will show to unzip files from a zip file  and store in folder

package test.facingissesonit.zip;

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 TestUnzip {

    public static void main(String[] args) {
        TestUnzip test = new TestUnzip();
        test.unzipFiles("C:\\test.zip", "C:\\output");

    }

    public void unzipFiles(String sourceZip, String outputLocation) {
        File outputFolder = null;
        ZipInputStream zis = null;
        ZipEntry ze = null;
        String zipFileName = null;
        File unzipFileName = null;
        FileOutputStream fos = null;
        try {
            // Create Output folder if not exist
            outputFolder = new File(outputLocation);
            if (!outputFolder.exists()) {
                outputFolder.mkdir();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        // Unzip files to output folder
        if (outputFolder != null) {

            byte[] buffer = new byte[1024];

            try {
                // Create Input Stream to read zip file
                zis = new ZipInputStream(new FileInputStream(sourceZip));
                // Get entry of next available zip file
                ze = zis.getNextEntry();

                while (ze != null) {

                    zipFileName = ze.getName();
                    // Path to file where unzip file will store
                    unzipFileName = new File(outputFolder + File.separator + zipFileName);

                    System.out.println("Unzip File : " + unzipFileName.getAbsoluteFile());
                    // Create directory structure if not exist
                    new File(unzipFileName.getParent()).mkdirs();
                    // Create unzip file on this directory
                    fos = new FileOutputStream(unzipFileName);
                    //Read zip file data in Buffer and write to file
                    int len;
                    while ((len = zis.read(buffer)) > 0) {
                        fos.write(buffer, 0, len);
                    }

                    fos.close();
                    ze = zis.getNextEntry();
                }

                System.out.println("Unzip Done for all files");
            } catch (IOException ex) {
                ex.printStackTrace();
            } finally {
                try {
                    if (zis != null) {
                        // Close current entry of file for reading
                        zis.closeEntry();
                        // close Zip and realease resources associated to it
                        zis.close();
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }

        }

    }
}