Java Unzip File unzip(final InputStream is, final File outputDirectory)

Here you can find the source of unzip(final InputStream is, final File outputDirectory)

Description

Unzip a zip file in a directory.

License

LGPL

Parameter

Parameter Description
is input stream of the zip file
outputDirectory output directory

Exception

Parameter Description
IOException if an error occurs while unzipping the file

Declaration

public static void unzip(final InputStream is, final File outputDirectory) throws IOException 

Method Source Code


//package com.java2s;
/*//  w w  w.  j  a  v a 2  s  .  c o m
 *                  Eoulsan development code
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public License version 2.1 or
 * later and CeCILL-C. This should be distributed with the code.
 * If you do not have a copy, see:
 *
 *      http://www.gnu.org/licenses/lgpl-2.1.txt
 *      http://www.cecill.info/licences/Licence_CeCILL-C_V1-en.txt
 *
 * Copyright for this code is held jointly by the Genomic platform
 * of the Institut de Biologie de l'?cole normale sup?rieure and
 * the individual authors. These should be listed in @author doc
 * comments.
 *
 * For more information on the Eoulsan project and its aims,
 * or to join the Eoulsan Google group, visit the home page
 * at:
 *
 *      http://outils.genomique.biologie.ens.fr/eoulsan
 *
 */

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

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;
import java.io.InputStream;

import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class Main {
    /** The default size of the buffer. */
    private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;

    /**
     * Unzip a zip file in a directory.
     * @param is input stream of the zip file
     * @param outputDirectory output directory
     * @throws IOException if an error occurs while unzipping the file
     */
    public static void unzip(final InputStream is, final File outputDirectory) throws IOException {

        if (is == null) {
            throw new IOException("The inputStream is null");
        }

        if (outputDirectory == null) {
            throw new IOException("The output directory is null");
        }

        if (!(outputDirectory.exists() && outputDirectory.isDirectory())) {
            throw new IOException("The output directory is invalid (" + outputDirectory + ")");
        }

        BufferedOutputStream dest = null;

        final ZipInputStream zis = new ZipInputStream(new BufferedInputStream(is));
        ZipEntry entry;

        while ((entry = zis.getNextEntry()) != null) {

            final File newFile = new File(outputDirectory + File.separator + entry.getName());

            if (entry.isDirectory()) {

                if (!newFile.exists()) {
                    if (!newFile.mkdirs()) {
                        throw new IOException("Cannot create directory: " + newFile);
                    }
                }

            } else {

                final File parentFile = newFile.getParentFile();
                if (!parentFile.exists()) {
                    if (!parentFile.mkdirs()) {
                        throw new IOException("Cannot create directory: " + parentFile);
                    }
                }

                int count;
                byte data[] = new byte[DEFAULT_BUFFER_SIZE];
                // write the files to the disk
                FileOutputStream fos = new FileOutputStream(newFile);
                dest = new BufferedOutputStream(fos, DEFAULT_BUFFER_SIZE);

                while ((count = zis.read(data, 0, DEFAULT_BUFFER_SIZE)) != -1) {
                    dest.write(data, 0, count);
                }

                dest.flush();
                dest.close();
            }

        }
        zis.close();
    }

    /**
     * Unzip a zip file in a directory.
     * @param zipFile The zip file
     * @param outputDirectory The output directory
     * @throws IOException if an issue occurs while unzipping the file
     */
    public static void unzip(final File zipFile, final File outputDirectory) throws IOException {

        if (zipFile == null) {
            throw new IOException("The zip file is null");
        }

        if (!(zipFile.exists() && zipFile.isFile())) {
            throw new IOException("Invalid zip file (" + zipFile.getName() + ")");
        }

        unzip(new FileInputStream(zipFile), outputDirectory);
    }
}

Related

  1. unzip(final byte[] zippedContent)
  2. unzip(final File dir, final ZipInputStream from)
  3. unzip(final File zip, final File dir)
  4. unzip(final File zipFile, final File destDir)
  5. unzip(final File zipFile, final String targetDir)
  6. unzip(final InputStream zipFile, final File targetFolder)
  7. unZip(final String result)
  8. unzip(final String zipFile, final String outputFolder, final boolean createFolder)