Java Unzip File unzip(File archiveFile, File destination)

Here you can find the source of unzip(File archiveFile, File destination)

Description

Unzip a given archive to a given destination.

License

Open Source License

Parameter

Parameter Description
archiveFile archive file to be unzipped
destination location to put the unzipped file

Exception

Parameter Description
IOException throws when fail to create the directory structure when unzipping a file.

Declaration

public static void unzip(File archiveFile, File destination)
        throws IOException 

Method Source Code

//package com.java2s;
/*/*from  w  ww.  j  av a2s  .  com*/
 *  Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 *  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 {
    private static final int BUFFER = 2048;

    /**
     * Unzip a given archive to a given destination.
     *
     * @param archiveFile archive file to be unzipped
     * @param destination location to put the unzipped file
     * @throws IOException throws when fail to create the directory structure when unzipping a file.
     */
    public static void unzip(File archiveFile, File destination)
            throws IOException {

        try (FileInputStream fis = new FileInputStream(archiveFile);
                ZipInputStream zis = new ZipInputStream(
                        new BufferedInputStream(fis))) {
            ZipEntry entry;

            while ((entry = zis.getNextEntry()) != null) {
                int count;
                byte data[] = new byte[BUFFER];
                File file = new File(destination, entry.getName());
                if (entry.getName().endsWith("/")) {
                    if (!file.exists() && !file.mkdirs()) {
                        throw new IOException(
                                "Failed to create directories at "
                                        + file.getAbsolutePath());
                    }
                    continue;
                }
                if (file.getParentFile() != null
                        && !file.getParentFile().exists()) {
                    if (!file.getParentFile().exists()
                            && !file.getParentFile().mkdirs()) {
                        throw new IOException(
                                "Failed to create directories at "
                                        + file.getAbsolutePath());
                    }
                }
                try (FileOutputStream fos = new FileOutputStream(file);
                        BufferedOutputStream dest = new BufferedOutputStream(
                                fos, BUFFER)) {
                    while ((count = zis.read(data, 0, BUFFER)) != -1) {
                        dest.write(data, 0, count);
                    }
                }
            }
        }
    }
}

Related

  1. unCompressGzipFile(String path)
  2. uncompressZipEntry(ZipInputStream zis, ZipEntry zipEntry, String dest)
  3. unzip(File aFile)
  4. unzip(File archive)
  5. unzip(File archive, File output)
  6. unzip(File archiveFile, File destination)
  7. unzip(File archiveFile, File targetDir, boolean skipRoot)
  8. unzip(File dest, String jar)
  9. unzip(File destDir, InputStream is)