Java Unzip File unzip(File zipFile, File destDir)

Here you can find the source of unzip(File zipFile, File destDir)

Description

Unpacks a zip file to the target directory.

License

Apache License

Parameter

Parameter Description
zipFile zip file
destDir destination directory

Exception

Parameter Description
IOException an exception

Declaration

public static void unzip(File zipFile, File destDir) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.*;
import java.util.Enumeration;

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

public class Main {
    /**/*from  www .j  ava2 s  .  co m*/
     * Unpacks a zip file to the target directory.
     * 
     * @param zipFile
     *            zip file
     * @param destDir
     *            destination directory
     * @throws IOException
     */
    public static void unzip(File zipFile, File destDir) throws IOException {
        ZipFile zip = new ZipFile(zipFile);
        try {
            Enumeration<?> en = zip.entries();
            int bufSize = 8196;
            while (en.hasMoreElements()) {
                ZipEntry entry = (ZipEntry) en.nextElement();
                File file = (destDir != null) ? new File(destDir, entry.getName()) : new File(entry.getName());
                if (entry.isDirectory()) {
                    if (!file.mkdirs()) {
                        if (file.isDirectory() == false) {
                            throw new IOException("Error creating directory: " + file);
                        }
                    }
                } else {
                    unzi(zip, bufSize, entry, file);
                }
            }
        } finally {
            zip.close();
        }
    }

    private static void unzi(ZipFile zip, int bufSize, ZipEntry entry, File file)
            throws IOException, FileNotFoundException {
        File parent = file.getParentFile();
        if (parent != null && !parent.exists()) {
            if (!parent.mkdirs()) {
                if (file.isDirectory() == false) {
                    throw new IOException("Error creating directory: " + parent);
                }
            }
        }

        InputStream in = zip.getInputStream(entry);
        try {
            OutputStream out = new BufferedOutputStream(new FileOutputStream(file), bufSize);
            try {
                copyPipe(in, out, bufSize);
            } finally {
                out.close();
            }
        } finally {
            in.close();
        }
    }

    private static void copyPipe(InputStream in, OutputStream out, int bufSizeHint) throws IOException {
        int read;
        byte[] buf = new byte[bufSizeHint];
        while ((read = in.read(buf, 0, bufSizeHint)) >= 0) {
            out.write(buf, 0, read);
        }
        out.flush();
    }
}

Related

  1. unzip(File zip, File toDir)
  2. unzip(File zip, String folder, File into)
  3. unZip(File zipf, String targetDir)
  4. unzip(File zipFile, File dest)
  5. unzip(File zipFile, File destDir)
  6. unzip(File zipFile, File destDir)
  7. unzip(File zipFile, File destDir)
  8. unzip(File zipFile, File destDir)
  9. unzip(File zipFile, File destination)