Java Unzip File unzip(File input, File outputDirectory)

Here you can find the source of unzip(File input, File outputDirectory)

Description

Unzips the file input and places its content in outputDirectory.

License

Open Source License

Parameter

Parameter Description
input a zip file.
outputDirectory a directory.

Exception

Parameter Description
ZipException an exception
IOException an exception

Declaration

public static void unzip(File input, File outputDirectory) throws ZipException, IOException 

Method Source Code


//package com.java2s;
/*//from  ww  w  .  j  a  v a2s.  c  o m
 * Daisy Pipeline (C) 2005-2008 Daisy Consortium
 * 
 * This library 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 library 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 library; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

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.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;

public class Main {
    /**
     * Unzips the file input and places its content in outputDirectory.
     * 
     * @param input
     *            a zip file.
     * @param outputDirectory
     *            a directory.
     * @throws ZipException
     * @throws IOException
     */
    public static void unzip(File input, File outputDirectory) throws ZipException, IOException {
        ZipFile zip = new ZipFile(input);
        for (Enumeration<? extends ZipEntry> entries = zip.entries(); entries.hasMoreElements();) {
            ZipEntry entry = entries.nextElement();

            if (entry.isDirectory()) {
                File dir = new File(outputDirectory, entry.getName());
                dir.mkdirs();
            } else {
                File out = new File(outputDirectory, entry.getName());
                out.getParentFile().mkdirs();

                FileOutputStream fos = new FileOutputStream(out);
                copy(zip.getInputStream(entry), fos);
            }
        }
    }

    /**
     * Writes the contents of in to out.
     * 
     * @param in
     *            the InputStream.
     * @param out
     *            the OutputStream.
     * @throws IOException
     */
    private static void copy(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[1024];
        int len;

        while ((len = in.read(buffer)) >= 0) {
            out.write(buffer, 0, len);
        }

        in.close();
        out.close();
    }
}

Related

  1. unzip(File file, String targetDirectory)
  2. unzip(File fileToUnzip, File destinationDirectory)
  3. unZip(File inFile, File unzipDir)
  4. unzip(File input)
  5. unzip(File input, File outputDir)
  6. unzip(File inputFile, File outputDir)
  7. unzip(File inputFile, File unzipDestFolder)
  8. unzip(File intoFolder, ZipFile zipFile)
  9. unzip(File jar, File target)