Java Unzip ZipFile unzip(ZipFile zipFile, File dest)

Here you can find the source of unzip(ZipFile zipFile, File dest)

Description

Unzip a zip file into a directory.

License

Open Source License

Parameter

Parameter Description
zipFile The zip file to be unzipped.
dest the destination directory.

Exception

Parameter Description
IOException if the destination does not exist or is not a directory, or if the zip file cannot be extracted.

Declaration

public static void unzip(ZipFile zipFile, File dest) throws IOException 

Method Source Code


//package com.java2s;
/*/*w ww  . jav  a 2 s.  c o  m*/
 * ProActive Parallel Suite(TM):
 * The Open Source library for parallel and distributed
 * Workflows & Scheduling, Orchestration, Cloud Automation
 * and Big Data Analysis on Enterprise Grids & Clouds.
 *
 * Copyright (c) 2007 - 2017 ActiveEon
 * Contact: contact@activeeon.com
 *
 * This library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License
 * as published by the Free Software Foundation: version 3 of
 * the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * If needed, contact us to obtain a release under GPL Version 2 or 3
 * or a different license than the AGPL.
 */

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.StringTokenizer;

import java.util.zip.ZipEntry;

import java.util.zip.ZipFile;

public class Main {
    /**
     * Unzip a zip file into a directory.
     * @param zipFile The zip file to be unzipped.
     * @param dest the destination directory.
     * @throws IOException if the destination does not exist or is not a directory, or if the zip file cannot be extracted.
     */
    public static void unzip(ZipFile zipFile, File dest) throws IOException {
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        if (dest.exists() && dest.isDirectory()) {
            while (entries.hasMoreElements()) {
                ZipEntry entry = entries.nextElement();
                if (!entry.isDirectory()) {
                    File destFile = new File(dest, entry.getName());
                    createFileWithPath(destFile);
                    InputStream in = new BufferedInputStream(zipFile.getInputStream(entry));
                    OutputStream out = new BufferedOutputStream(new FileOutputStream(destFile));
                    byte[] buffer = new byte[2048];
                    for (;;) {
                        int nBytes = in.read(buffer);
                        if (nBytes <= 0)
                            break;
                        out.write(buffer, 0, nBytes);
                    }
                    out.flush();
                    out.close();
                    in.close();
                }
            }
        } else {
            throw new IOException(
                    "Destination " + dest.getAbsolutePath() + " is not a directory or does not exist");
        }
    }

    protected static void createFileWithPath(File f) throws IOException {
        String absPath = f.getAbsolutePath();
        StringTokenizer parser = new StringTokenizer(absPath, File.separator);
        StringBuffer globalPath = new StringBuffer(File.separator);
        while (parser.countTokens() > 1) {
            globalPath = globalPath.append(parser.nextToken() + File.separator);
            File currentDir = new File(globalPath.toString());
            if (!currentDir.exists()) {
                if (!currentDir.mkdir()) {
                    throw new IOException("Cannot create directory " + currentDir.getAbsolutePath());
                }
            }
        }
        if (!f.createNewFile()) {
            throw new IOException("Cannot create file " + f.getAbsolutePath());
        }
    }
}

Related

  1. unzip(File srcFile, File toDir)
  2. unzip(final File zip, final File patchDir)
  3. unzip(final File zipFile, final String suffix)
  4. unzip(ZipEntry entry, ZipFile zipfile, File explodedDir)
  5. unzip(ZipFile zip, File fileDir)
  6. unzip(ZipFile zipFile, File destDir)
  7. unzip(ZipFile zipFile, File destDirectory)
  8. unzip(ZipFile zipFile, File dstDir)
  9. unZip(ZipFile zipFile, File outputDir)