Java Unzip to Folder unzipZipFile(ZipFile zip, String directory)

Here you can find the source of unzipZipFile(ZipFile zip, String directory)

Description

unzip Zip File

License

BSD License

Declaration

@SuppressWarnings("unchecked")
    private static Collection<String> unzipZipFile(ZipFile zip, String directory)
            throws FileNotFoundException, IOException 

Method Source Code


//package com.java2s;
/*L/* w ww . j  ava  2s . com*/
 * Copyright The General Hospital Corporation d/b/a Massachusetts General Hospital
 *
 * Distributed under the OSI-approved BSD 3-Clause License.
 * See http://ncip.github.com/digital-model-repository/LICENSE.txt for details.
 */

import java.io.BufferedOutputStream;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import java.util.ArrayList;
import java.util.Collection;

import java.util.Enumeration;

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

public class Main {
    @SuppressWarnings("unchecked")
    private static Collection<String> unzipZipFile(ZipFile zip, String directory)
            throws FileNotFoundException, IOException {
        Collection<String> returnCollection = new ArrayList<String>();
        Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) zip.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) entries.nextElement();
            copyInputStream(zip.getInputStream(entry), new BufferedOutputStream(
                    new FileOutputStream(directory + System.getProperty("file.separator") + entry.getName())));
            returnCollection.add(entry.getName());
        }
        zip.close();
        return returnCollection;
    }

    private static final void copyInputStream(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. unzipToDirectory(File file, File dir)
  2. unzipToFolder(final InputStream inputStream, final File outputFolder)
  3. unzipToFolder(final InputStream inputStream, final File outputFolder)
  4. unzipToFolder(String folderPath, InputStream source)
  5. unzipToFolder(ZipInputStream zipInputStream, File dirToExtractTo)