Java Unzip InputStream decompressZipArchive(final File zipFile, final File rootDirectoryToUnZip)

Here you can find the source of decompressZipArchive(final File zipFile, final File rootDirectoryToUnZip)

Description

Takes a zip file and decompress it to the given root directory.

License

Apache License

Declaration

public static void decompressZipArchive(final File zipFile, final File rootDirectoryToUnZip)
        throws IOException 

Method Source Code


//package com.java2s;
/*/*from w w  w  .ja  v  a 2s  .co  m*/
 * Copyright 2011-2015 B2i Healthcare Pte Ltd, http://b2i.sg
 * 
 * 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.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import java.io.IOException;

import java.util.zip.ZipEntry;

import java.util.zip.ZipInputStream;

public class Main {
    /**
     * Takes a zip file and decompress it to the given root directory.
     * If the directory where to put the contents of the zip file is not exist it creates it.
     *
     */
    public static void decompressZipArchive(final File zipFile, final File rootDirectoryToUnZip)
            throws IOException {

        final int BUFFER = 2048;

        if (rootDirectoryToUnZip.exists() && rootDirectoryToUnZip.isFile()) {
            throw new IllegalArgumentException("Passed argument points to an existing file not a directory.");
        }

        if (!rootDirectoryToUnZip.exists()) {
            rootDirectoryToUnZip.mkdir();
        }

        final FileInputStream fis = new FileInputStream(zipFile);
        final ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));

        ZipEntry entry = zis.getNextEntry();

        while (entry != null) {

            File newFile = new File(rootDirectoryToUnZip, entry.getName());

            if (entry.isDirectory()) {
                newFile.mkdir();
            } else {

                File parentFile = newFile.getParentFile();
                if (parentFile != null && !parentFile.exists()) {
                    parentFile.mkdirs();
                }

                int count;
                final byte data[] = new byte[BUFFER];

                final FileOutputStream fos = new FileOutputStream(newFile);
                final BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);

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

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

            entry = zis.getNextEntry();
        }

        zis.close();
        fis.close();
    }
}

Related

  1. decompress(InputStream input, File destDir)
  2. decompress(InputStream inputStream)
  3. decompress(InputStream is, OutputStream os)
  4. decompressFile(InputStream is, OutputStream os)
  5. decompressStream(InputStream input)
  6. unzip(InputStream from, String to, String pattern)
  7. unzip(InputStream in)
  8. unzip(InputStream in, File destination)
  9. unzip(InputStream in, File toDir)