Java Unzip to Folder unzip(String zip, String dest)

Here you can find the source of unzip(String zip, String dest)

Description

Unzip a zip file into the destination directory

License

Apache License

Parameter

Parameter Description
zip a parameter
dest a parameter

Declaration

public static boolean unzip(String zip, String dest) 

Method Source Code

//package com.java2s;
/**************************************************************
 * /* w w w  .j a  v a2s  .  co m*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.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 {
    /**
     * Unzip a zip file into the destination directory
     * 
     * @param zip
     * @param dest
     * @return
     */
    public static boolean unzip(String zip, String dest) {
        return unzip(new File(zip), new File(dest));
    }

    /**
     * Unzip a zip file into the destination directory
     * 
     * @param zipFile
     * @param destFile
     * @return
     */
    public static boolean unzip(File zipFile, File destFile) {
        ZipInputStream zin = null;
        FileOutputStream fos = null;
        try {
            zin = new ZipInputStream(new FileInputStream(zipFile));
            ZipEntry entry;
            while ((entry = zin.getNextEntry()) != null) {
                File entryFile = new File(destFile, entry.getName());
                if (entry.isDirectory()) {
                    entryFile.mkdirs();
                } else {
                    entryFile.getParentFile().mkdirs();
                    fos = new FileOutputStream(entryFile);
                    byte[] b = new byte[1024];
                    int len = 0;
                    while ((len = zin.read(b)) != -1) {
                        fos.write(b, 0, len);
                    }
                    fos.close();
                    zin.closeEntry();
                }

                if (entry.getTime() >= 0)
                    entryFile.setLastModified(entry.getTime());
            }
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            // log.log(Level.SEVERE,
            // MessageFormat.format(" Fail! Unzip [{0}] -> [{1}]", zipFile,
            // destFile), e);
            return false;
        } finally {
            if (zin != null)
                try {
                    zin.close();
                } catch (IOException e) {
                }
            if (fos != null)
                try {
                    fos.close();
                } catch (IOException e) {
                }
        }
    }
}

Related

  1. unzip(String sourceFileName, String destPath)
  2. unzip(String sourceZip, String targetDirectory)
  3. unzip(String src, String dest, PrintStream stream)
  4. unzip(String sZip)
  5. unzip(String toUnpackName)
  6. unzip(String zip, String folder)
  7. unzip(String zip, String path)
  8. unzip(String zip, String unzipDir, int bufferSize)
  9. Unzip(String zipFile, String outputDirectory)