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

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

Description

unzip

License

Open Source License

Declaration

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

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2012 OpenLegacy Inc.//  www  .j  a  v  a2s .  c o  m
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     OpenLegacy Inc. - initial API and implementation
 *******************************************************************************/

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.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class Main {
    private static int BUFFER_SIZE = 2048;

    public static void unzip(String zipFileName, String targetPath) throws IOException {

        ZipFile zipFile = new ZipFile(zipFileName);

        Enumeration<? extends ZipEntry> entries = zipFile.entries();

        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();

            if (entry.isDirectory()) {
                (new File(targetPath, entry.getName())).mkdir();
            }

            File outputFile = new File(targetPath, entry.getName());

            File outputParentFile = new File(outputFile.getParent());
            outputParentFile.mkdirs();
            if (!entry.isDirectory()) {
                unzipStream(zipFile.getInputStream(entry),
                        new BufferedOutputStream(new FileOutputStream(outputFile)));
            }
        }

        zipFile.close();
    }

    private static final void unzipStream(InputStream in, OutputStream out) throws IOException {
        int len;
        byte[] buffer = new byte[BUFFER_SIZE];

        while ((len = in.read(buffer)) >= 0) {
            out.write(buffer, 0, len);
        }

        in.close();
        out.close();
    }
}

Related

  1. unzip(String zipFile, String targetFolder)
  2. unzip(String zipFile, String targetFolder, String... fileSuffixes)
  3. unzip(String zipFile, String targetPath)
  4. unzip(String zipFileName, String outputDirectory)
  5. unzip(String zipFileName, String targetFolderPath)
  6. unzip(String zipFileName, String unzipdir)
  7. unzipFile(String zipFile, File outputFolder)
  8. unzipFile(String zipFile, String extDir)
  9. unZipFile(String zipFile, String outputFolder)