Java Unzip to Folder unzip(String zipFile, String targetPath)

Here you can find the source of unzip(String zipFile, String targetPath)

Description

unzip

License

Apache License

Declaration

public static void unzip(String zipFile, String targetPath) throws IOException 

Method Source Code


//package com.java2s;
/*/*www .j  av a  2 s.com*/
 * Copyright 2011 ancoron.
 *
 * 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.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class Main {
    private final static int BUFFER = 4 * 1024;
    private static final Logger log = Logger.getLogger("glassfishtest");

    public static void unzip(String zipFile, String targetPath) throws IOException {
        log.log(Level.INFO, "Extracting {0} ...", zipFile);
        log.log(Level.INFO, "...target directory: {0}", targetPath);
        File path = new File(targetPath);
        path.delete();
        path.mkdirs();

        BufferedOutputStream dest = null;
        FileInputStream fis = new FileInputStream(zipFile);
        ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            if (entry.isDirectory()) {
                File f = new File(targetPath + "/" + entry.getName());
                f.mkdirs();
                continue;
            }
            int count;
            byte data[] = new byte[BUFFER];

            // write the files to the disk
            FileOutputStream fos = new FileOutputStream(targetPath + "/" + entry.getName());
            dest = new BufferedOutputStream(fos, BUFFER);
            while ((count = zis.read(data, 0, BUFFER)) != -1) {
                dest.write(data, 0, count);
            }
            dest.flush();
            dest.close();
        }
        zis.close();
    }
}

Related

  1. unzip(String zip, String path)
  2. unzip(String zip, String unzipDir, int bufferSize)
  3. Unzip(String zipFile, String outputDirectory)
  4. unzip(String zipFile, String targetFolder)
  5. unzip(String zipFile, String targetFolder, String... fileSuffixes)
  6. unzip(String zipFileName, String outputDirectory)
  7. unzip(String zipFileName, String targetFolderPath)
  8. unzip(String zipFileName, String targetPath)
  9. unzip(String zipFileName, String unzipdir)