Java Unzip InputStream unzip(InputStream input, File dest)

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

Description

Unzip the given inputstream into a directory.

License

Open Source License

Declaration

public static void unzip(InputStream input, File dest) throws IOException 

Method Source Code

//package com.java2s;
/**/* ww w .  j  a  v a 2  s .  com*/
 * Copyright (c) 2005-2007 Intalio inc.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Intalio inc. - initial API and implementation
 */

import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.File;

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

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

public class Main {
    /**
     * Unzip the given inputstream into a directory.
     */
    public static void unzip(InputStream input, File dest) throws IOException {
        ZipInputStream zis = new ZipInputStream(input);
        ZipEntry entry;
        try {
            while ((entry = zis.getNextEntry()) != null) {
                if (entry.isDirectory()) {
                    File dir = new File(dest, entry.getName());
                    dir.mkdir();
                    if (!dir.exists() || !dir.isDirectory()) {
                        throw new IOException("Error creating directory: " + dir);
                    }
                    continue;
                }
                File destFile = new File(dest, entry.getName());
                File parent = destFile.getParentFile();
                if (!parent.exists())
                    parent.mkdirs();
                if (!parent.exists() || !parent.isDirectory()) {
                    throw new IOException("Error creating directory: " + parent);
                }
                OutputStream out = new BufferedOutputStream(new FileOutputStream(destFile));
                try {
                    copyStream(zis, out);
                } finally {
                    out.close();
                }
            }
        } finally {
            zis.close();
        }
    }

    /**
     * Copies the contents of the <code>InputStream</code> into the <code>OutputStream</code>.
     */
    public static void copyStream(InputStream input, OutputStream output) {
        try {
            byte[] bytes = new byte[4096];
            int bytesRead = 0;
            while ((bytesRead = input.read(bytes)) >= 0) {
                output.write(bytes, 0, bytesRead);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Close stream, ignoring any possible IOException.
     */
    public static void close(Closeable c) {
        try {
            if (c != null)
                c.close();
        } catch (IOException except) {
            // ignore
        }
    }
}

Related

  1. decompressZipArchive(final File zipFile, final File rootDirectoryToUnZip)
  2. unzip(InputStream from, String to, String pattern)
  3. unzip(InputStream in)
  4. unzip(InputStream in, File destination)
  5. unzip(InputStream in, File toDir)
  6. unzip(InputStream input, String targetDir)
  7. unzip(InputStream inputStream, String destDirectory)
  8. unzip(InputStream inputStream, String outputFolder)
  9. unzip(InputStream is, File destDir)