Java Unzip to Folder unzipFile(String zipFile, File outputFolder)

Here you can find the source of unzipFile(String zipFile, File outputFolder)

Description

Unzip a file into a specified output folder

License

Apache License

Declaration

static void unzipFile(String zipFile, File outputFolder)
        throws IOException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

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 Main {
    /**//  www.j a  v a2 s .  c o  m
     * Unzip a file into a specified output folder
     */
    static void unzipFile(String zipFile, File outputFolder)
            throws IOException {

        byte[] buffer = new byte[1024];

        // create output directory is not exists
        if (!outputFolder.exists()) {
            outputFolder.mkdir();
        }

        // get the zip file content
        ZipInputStream zis = new ZipInputStream(
                new FileInputStream(zipFile));
        // get the zipped file list entry
        ZipEntry ze = zis.getNextEntry();

        while (ze != null) {

            String fileName = ze.getName();
            File newFile = new File(outputFolder + File.separator
                    + fileName);

            System.out
                    .println("Unzipping to: " + newFile.getAbsoluteFile());

            // create all non existing folders
            // otherwise you will get FileNotFoundException for compressed folder
            if (ze.isDirectory()) {
                new File(newFile.getParent()).mkdirs();
            } else {
                FileOutputStream fos = null;
                new File(newFile.getParent()).mkdirs();
                fos = new FileOutputStream(newFile);
                int len;
                while ((len = zis.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }
                fos.close();
            }
            ze = zis.getNextEntry();
        }

        zis.closeEntry();
        zis.close();

        System.out.println("Unzip complete");
    }
}

Related

  1. unzip(String zipFile, String targetPath)
  2. unzip(String zipFileName, String outputDirectory)
  3. unzip(String zipFileName, String targetFolderPath)
  4. unzip(String zipFileName, String targetPath)
  5. unzip(String zipFileName, String unzipdir)
  6. unzipFile(String zipFile, String extDir)
  7. unZipFile(String zipFile, String outputFolder)
  8. unzipFile(String zipFile, String outputFolder)
  9. unzipFile(String zipFileName, String outputDir)