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

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

Description

unzip File

License

Open Source License

Declaration

static public void unzipFile(String zipFile, String outputFolder)
            throws Exception 

Method Source Code

//package com.java2s;
/*//w ww .ja va  2  s.co  m
 * ? Copyright IBM Corp. 2015
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at:
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 * implied. See the License for the specific language governing 
 * permissions and limitations under the License.
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import java.util.zip.ZipEntry;

import java.util.zip.ZipInputStream;

public class Main {
    static public void unzipFile(String zipFile, String outputFolder)
            throws Exception {
        byte[] buf = new byte[1024];
        ZipInputStream zis = null;

        try {
            zis = new ZipInputStream(new FileInputStream(zipFile));
            ZipEntry ze = zis.getNextEntry();

            while (ze != null) {
                String fileName = ze.getName();
                File newFile = new File(outputFolder + File.separator
                        + fileName);

                // Create sub-folders
                new File(newFile.getParent()).mkdirs();

                if (ze.isDirectory()) {
                    // Handles empty folders in the zip file
                    newFile.mkdirs();
                } else {
                    FileOutputStream fos = null;
                    try {
                        fos = new FileOutputStream(newFile);
                        int len;
                        while ((len = zis.read(buf)) > 0) {
                            fos.write(buf, 0, len);
                        }
                    } finally {
                        if (fos != null) {
                            fos.flush();
                            fos.close();
                        }
                    }
                }
                ze = zis.getNextEntry();
            }
        } finally {
            if (zis != null) {
                zis.closeEntry();
                zis.close();
            }
        }
    }
}

Related

  1. unzip(String zipFileName, String targetPath)
  2. unzip(String zipFileName, String unzipdir)
  3. unzipFile(String zipFile, File outputFolder)
  4. unzipFile(String zipFile, String extDir)
  5. unZipFile(String zipFile, String outputFolder)
  6. unzipFile(String zipFileName, String outputDir)
  7. unzipFile(String zipFilePath, String releasePath)
  8. unzipFile(String zipFilePath, String targetPath)
  9. unzipFile(ZipFile zipFile, File destinationDir)