Java Unzip to Folder unzipFile(String zipFileName, String outputDir)

Here you can find the source of unzipFile(String zipFileName, String outputDir)

Description

Unzip a given zip file to a specified output directory.

License

Apache License

Parameter

Parameter Description
zipFileName a parameter
outputDir a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void unzipFile(String zipFileName, String outputDir) throws IOException 

Method Source Code


//package com.java2s;
/*/*from w w w.j av a 2 s  .co m*/
 *
 * Copyright SHMsoft, Inc. 
 *
 * 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.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;

import java.util.Enumeration;

import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class Main {
    /**
     * Unzip a given zip file to a specified output directory.
     *
     * @param zipFileName
     * @param outputDir
     * @throws IOException
     */
    public static void unzipFile(String zipFileName, String outputDir) throws IOException {

        ZipFile zipFile = new ZipFile(new File(zipFileName));
        @SuppressWarnings("rawtypes")
        Enumeration e = zipFile.entries();

        while (e.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) e.nextElement();
            File destinationFilePath = new File(outputDir, entry.getName());

            // create directories if required.
            destinationFilePath.getParentFile().mkdirs();

            // if the entry is not a directory, extract it.
            if (!entry.isDirectory()) {
                /*
                 * Get the InputStream for current entry of the zip file using
                 * 
                 * InputStream getInputStream(Entry entry) method.
                 */
                BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));

                int b;
                byte buffer[] = new byte[1024];

                /*
                 * read the current entry from the zip file, extract it and
                 * write the extracted file.
                 */
                FileOutputStream fos = new FileOutputStream(destinationFilePath);
                BufferedOutputStream bos = new BufferedOutputStream(fos, 1024);

                while ((b = bis.read(buffer, 0, 1024)) != -1) {
                    bos.write(buffer, 0, b);
                }

                // flush the output stream and close it.
                bos.flush();
                bos.close();

                // close the input stream.
                bis.close();
            }
        }
    }
}

Related

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