Java Unzip to Folder unzip(String sourceZip, String targetDirectory)

Here you can find the source of unzip(String sourceZip, String targetDirectory)

Description

unzip

License

Apache License

Declaration

public static void unzip(String sourceZip, String targetDirectory) throws Exception 

Method Source Code


//package com.java2s;
/*//from  www.  j a  v  a  2  s.c  o m
 *    Copyright 2005 The Regents of the University of Michigan
 *
 * 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.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class Main {
    public static void unzip(String sourceZip, String targetDirectory) throws Exception {

        // read the ZIP
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        ZipInputStream zis = null;

        try {
            // make the streams
            fis = new FileInputStream(new File(sourceZip));
            bis = new BufferedInputStream(fis);
            zis = new ZipInputStream(bis);

            // read the entries
            for (ZipEntry ze = zis.getNextEntry(); ze != null; ze = zis.getNextEntry()) {

                // make the file
                File f = new File(targetDirectory + File.separator + ze.getName());
                if (ze.isDirectory()) {
                    f.mkdirs();
                    continue;
                } else {
                    if (f.getParentFile() != null) {
                        f.getParentFile().mkdirs();
                    }
                    f.createNewFile();
                }

                FileOutputStream fos = null;
                BufferedOutputStream bos = null;

                try {
                    fos = new FileOutputStream(f);
                    bos = new BufferedOutputStream(fos);

                    while (zis.available() > 0) {
                        bos.write(zis.read());
                    }
                } finally {
                    try {
                        bos.close();
                        fos.close();
                    } catch (Exception e) {
                    }
                }
            }
        } finally {
            try {
                zis.close();
                bis.close();
                fis.close();
            } catch (Exception e) {
            }
        }
    }
}

Related

  1. unZip(String pathDest, BufferedInputStream buffInputStream)
  2. unzip(String sourceFile, String destDir)
  3. unzip(String sourceFile, String destDir)
  4. unzip(String sourceFile, String toDir)
  5. unzip(String sourceFileName, String destPath)
  6. unzip(String src, String dest, PrintStream stream)
  7. unzip(String sZip)
  8. unzip(String toUnpackName)
  9. unzip(String zip, String dest)