Java Unzip to Folder unzip(String zip, String unzipDir, int bufferSize)

Here you can find the source of unzip(String zip, String unzipDir, int bufferSize)

Description

Unzips a zipFile in unzip directory.

License

Open Source License

Parameter

Parameter Description
zip a parameter
unzipDir a parameter
bufferSize a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void unzip(String zip, String unzipDir, int bufferSize) throws IOException 

Method Source Code

//package com.java2s;
/*//from  ww  w .j av a 2 s  .  co  m
 * Copyright (c) Fiorano Software Pte. Ltd. and affiliates. All rights reserved. http://www.fiorano.com
 * The software in this package is published under the terms of the CPAL v1.0
 * license, a copy of which has been included with this distribution in the
 * LICENSE.txt file.
 */

import java.io.*;

import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class Main {
    private static boolean DEBUG = false;

    /**
     * Unzips a zipFile in unzip directory.
     *
     * @param zip
     * @param unzipDir
     * @param bufferSize
     * @throws IOException
     */
    public static void unzip(String zip, String unzipDir, int bufferSize) throws IOException {
        // Initialize the unzip Dir.
        createDirs(unzipDir);

        ZipFile zipFile = new ZipFile(zip);
        try {
            Enumeration entries = zipFile.entries();

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

                // File
                File file = new File(unzipDir, entry.getName());

                if (entry.isDirectory()) {
                    // Assume directories are stored parents first then children.
                    if (DEBUG)
                        System.out.println("extracting.directory.0");

                    if (!file.exists()) {
                        boolean created = file.mkdir();

                        if (DEBUG)
                            System.out.println("directory.0.created.1");
                    }

                    continue;
                }

                if (DEBUG)
                    System.out.println("extracting.file.0");

                BufferedOutputStream bos = null;
                try {
                    // write entry on file-system.
                    bos = new BufferedOutputStream(new FileOutputStream(file));
                    InputStream entryStream = zipFile.getInputStream(entry);

                    copyInputStream(entryStream, bos, bufferSize);
                } finally {
                    if (bos != null)
                        bos.close();
                }
            }
        } finally {
            if (zipFile != null)
                zipFile.close();
        }
    }

    /**
     * Create directory including all the parent directories.
     *
     * @param dirName
     * @return
     */
    public static boolean createDirs(String dirName) {
        File dir = new File(dirName);

        if (dir.exists())
            return true;

        boolean created = createDirs(dir.getParent());

        if (!created)
            return false;

        return dir.mkdir();
    }

    /**
     * Copies inputStream on OutputStream.
     *
     * @param in
     * @param out
     * @param bufferSize
     * @throws IOException
     */
    private final static void copyInputStream(InputStream in, OutputStream out, int bufferSize) throws IOException {
        byte[] buffer = new byte[bufferSize];
        int len;

        try {
            while ((len = in.read(buffer)) >= 0)
                out.write(buffer, 0, len);
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                // ignore
            }
            try {
                out.close();
            } catch (IOException e) {
                // ignore
            }
        }
    }
}

Related

  1. unzip(String sZip)
  2. unzip(String toUnpackName)
  3. unzip(String zip, String dest)
  4. unzip(String zip, String folder)
  5. unzip(String zip, String path)
  6. Unzip(String zipFile, String outputDirectory)
  7. unzip(String zipFile, String targetFolder)
  8. unzip(String zipFile, String targetFolder, String... fileSuffixes)
  9. unzip(String zipFile, String targetPath)