Java Unzip ZipFile unzip(ZipFile zipFile, File destDirectory)

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

Description

Unzips a zipFile to the specified directory.

License

Open Source License

Parameter

Parameter Description
zipFile The file to unzip
destDirectory The directory to unzip the file

Exception

Parameter Description
Exception If an error occurs during the unzipping process

Declaration

public static void unzip(ZipFile zipFile, File destDirectory) throws Exception 

Method Source Code

//package com.java2s;
/*//from  www .  j a  va  2  s . co m
 * JBoss, Home of Professional Open Source.
 * Copyright 2007, Red Hat Middleware LLC, and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

import java.io.File;

import java.io.FileOutputStream;

import java.io.InputStream;
import java.io.OutputStream;

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

public class Main {
    /**
     * Unzips a zipFile to the specified directory.
     * @param zipFile The file to unzip
     * @param destDirectory The directory to unzip the file
     * @throws Exception If an error occurs during the unzipping process
     */
    public static void unzip(ZipFile zipFile, File destDirectory) throws Exception {
        if (!destDirectory.exists()) {
            destDirectory.mkdir();
        } else {
            if (!destDirectory.isDirectory()) {
                throw new Exception("The destDirectory file already exists and is not a directory");
            }
        }

        Enumeration enumer = zipFile.entries();
        while (enumer.hasMoreElements()) {
            ZipEntry zipEntry = (ZipEntry) enumer.nextElement();
            if (zipEntry.isDirectory()) {
                File dir = new File(destDirectory, zipEntry.toString());
                dir.mkdir();
            } else {
                File file = new File(destDirectory, zipEntry.toString());
                InputStream is = zipFile.getInputStream(zipEntry);
                OutputStream os = new FileOutputStream(file);
                int c;
                while ((c = is.read()) != -1) {
                    os.write(c);
                }
                is.close();
                os.close();
            }
        }
    }
}

Related

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