Java Unzip to Folder unzip(String inputZipPath, String destinationDirectory)

Here you can find the source of unzip(String inputZipPath, String destinationDirectory)

Description

unzip

License

Apache License

Declaration

public static void unzip(String inputZipPath, String destinationDirectory) throws IOException 

Method Source Code


//package com.java2s;
/*/*www. j a  va  2 s .  c om*/
 * Copyright 2013 The Trustees of Indiana University
 *
 * 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.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class Main {
    public static void unzip(String inputZipPath, String destinationDirectory) throws IOException {

        int BUFFER = 2048;
        List zipFiles = new ArrayList();
        File sourceZipFile = new File(inputZipPath);
        File unzipDestinationDirectory = new File(destinationDirectory);
        unzipDestinationDirectory.mkdir();

        ZipFile zipFile;

        zipFile = new ZipFile(sourceZipFile, ZipFile.OPEN_READ);

        Enumeration zipFileEntries = zipFile.entries();

        while (zipFileEntries.hasMoreElements()) {

            ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();

            String currentEntry = entry.getName();

            File destFile = new File(unzipDestinationDirectory.getAbsolutePath() + "/" + currentEntry);

            if (currentEntry.endsWith(".zip")) {
                zipFiles.add(destFile.getAbsolutePath());
            }

            File destinationParent = destFile.getParentFile();

            destinationParent.mkdirs();

            try {

                if (!entry.isDirectory()) {
                    BufferedInputStream is = new BufferedInputStream(zipFile.getInputStream(entry));
                    int currentByte;

                    byte data[] = new byte[BUFFER];

                    FileOutputStream fos = new FileOutputStream(destFile);
                    BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);

                    while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
                        dest.write(data, 0, currentByte);
                    }
                    dest.flush();
                    dest.close();
                    is.close();
                }
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
        zipFile.close();

        for (Iterator iter = zipFiles.iterator(); iter.hasNext();) {
            String zipName = (String) iter.next();
            unzip(zipName,
                    destinationDirectory + File.separatorChar + zipName.substring(0, zipName.lastIndexOf(".zip")));
        }
    }
}

Related

  1. unzip(String compressedStr)
  2. unzip(String file, String destinationdir)
  3. unzip(String filePath, String unzipPath)
  4. unzip(String inFilePath)
  5. unzip(String inFilePath, String outFilePath)
  6. unzip(String path, int buffer)
  7. unZip(String pathDest, BufferedInputStream buffInputStream)
  8. unzip(String sourceFile, String destDir)
  9. unzip(String sourceFile, String destDir)