Some days ago, I had to decompress a zipped file, as usual before reinvent the wheel I googled just a little and I found an interesting class. I made only some minor changes (thanks to netbeans) to the class and here you are the result:

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;

java-logo-ruby-style

public class UnZip {
    /**
     * Unzip it
     *
     * @param zipFile input zip file
     * @param outputFolder zip file output folder
     */
    public void unZipIt(String zipFile, String outputFolder) {
        byte[] buffer = new byte[1024];
        try {
            //create output directory is not exists
            File folder = new File(outputFolder);
            if (!folder.exists()) {
                folder.mkdir();
            }
            //get the zipped file list entry
            try (
                    //get the zip file content
                ZipInputStream zipinputstream = new ZipInputStream(new FileInputStream(zipFile))) {
                //get the zipped file list entry
                ZipEntry zipentry = zipinputstream.getNextEntry();
                while (zipentry != null) {
                    String fileName = zipentry.getName();
                    File newFile = new File(outputFolder + File.separator + fileName);
                    System.out.println("file unzip : " + newFile.getAbsoluteFile());
                    //create all non exists folders
                    //else you will hit FileNotFoundException for compressed folder
                    new File(newFile.getParent()).mkdirs();
                    try (FileOutputStream fos = new FileOutputStream(newFile)) {
                        int len;
                        while ((len = zipinputstream.read(buffer)) > 0) {
                            fos.write(buffer, 0, len);
                        }
                    }
                    zipentry = zipinputstream.getNextEntry();
                }
                zipinputstream.closeEntry();
            }
            System.out.println("Done");
        } catch (IOException ex) {
        }
    }
}

Add this class to your project and then when you need it, simple add the following two lines:

    UnZip unZip = new UnZip();


    unZip.unZipIt("source.zip","PathToDestinationDirectory");

 

the original sources are at http://www.mkyong.com/java/how-to-decompress-files-from-a-zip-file/

gg1