Java Unzip File unzip(final File dir, final ZipInputStream from)

Here you can find the source of unzip(final File dir, final ZipInputStream from)

Description

Copy a zip stream to the given directory.

License

Apache License

Parameter

Parameter Description
dir The target directory.
from The zip input stream to copy.

Declaration

public static void unzip(final File dir, final ZipInputStream from) throws IOException 

Method Source Code

//package com.java2s;
/*/*from  www.  j  a v  a  2 s  .com*/
 * Copyright (C) 2015 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.BufferedOutputStream;
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 {
    private static final int BUF_SIZE = 1024;

    /**
     * Copy a zip stream to the given directory.
     * 
     * @param dir
     *          The target directory.
     * @param from
     *          The zip input stream to copy.
     */
    public static void unzip(final File dir, final ZipInputStream from) throws IOException {
        ZipEntry entry;
        while ((entry = from.getNextEntry()) != null) {
            if (entry.isDirectory()) {
                new File(dir, entry.getName()).mkdirs();
            } else {
                final File file = new File(dir, entry.getName());
                file.getParentFile().mkdirs();
                file.createNewFile();
                copyStreamToFile(file, from);
                from.closeEntry();
            }
        }
    }

    /**
     * Copy the contents of a stream to the given file.
     * 
     * @param to
     *          A file (which may not yet exist).
     * @param from
     *          The stream from which to copy (must be open).
     */
    public static void copyStreamToFile(final File to, final InputStream from) throws IOException {
        if (!to.exists()) {
            to.createNewFile();
        }
        BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(to));

        copyStream(writer, from);

        writer.close();
    }

    /**
     * Copy one stream to another until the input stream has no more data.
     * 
     * @param to
     *          The open stream to write to.
     * @param from
     *          The open stream to read from.
     */
    public static void copyStream(final OutputStream to, final InputStream from) throws IOException {
        final byte[] buf = new byte[BUF_SIZE];
        int len = from.read(buf);
        while (len > 0) {
            to.write(buf, 0, len);
            len = from.read(buf);
        }
    }
}

Related

  1. unZip(File zipPath, File destPath)
  2. unzip(File zippedFile)
  3. unzip(File zippedFile, File outputDirectory)
  4. unzip(File zippedFile, File targetDir)
  5. unzip(final byte[] zippedContent)
  6. unzip(final File zip, final File dir)
  7. unzip(final File zipFile, final File destDir)
  8. unzip(final File zipFile, final String targetDir)
  9. unzip(final InputStream is, final File outputDirectory)