Java Unzip File unzip(File file, File directory)

Here you can find the source of unzip(File file, File directory)

Description

Unpacks a zip file into a directory.

License

LGPL

Parameter

Parameter Description
file The zip file
directory The directory

Exception

Parameter Description
IOException In case of a reading, writing or decompression error

Declaration

public static void unzip(File file, File directory) throws IOException 

Method Source Code

//package com.java2s;
/**/*from  w w  w .j ava  2  s  .  c o m*/
 * Copyright 2009-2016 Three Crickets LLC.
 * <p>
 * The contents of this file are subject to the terms of the LGPL version 3.0:
 * http://www.gnu.org/copyleft/lesser.html
 * <p>
 * Alternatively, you can obtain a royalty free commercial license with less
 * limitations, transferable or non-transferable, directly from Three Crickets
 * at http://threecrickets.com/
 */

import java.io.BufferedInputStream;
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 {
    /**
     * Unpacks a zip file into a directory.
     * 
     * @param file
     *        The zip file
     * @param directory
     *        The directory
     * @throws IOException
     *         In case of a reading, writing or decompression error
     */
    public static void unzip(File file, File directory) throws IOException {
        ZipFile zipFile = new ZipFile(file);
        try {
            Enumeration<? extends ZipEntry> entries = zipFile.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = entries.nextElement();
                File entryFile = new File(directory, entry.getName());

                if (entry.isDirectory())
                    entryFile.mkdirs();
                else {
                    InputStream in = zipFile.getInputStream(entry);
                    try {
                        OutputStream out = new FileOutputStream(entryFile);
                        try {
                            copyStream(in, out);
                        } finally {
                            out.close();
                        }
                    } finally {
                        in.close();
                    }
                }
            }
        } finally {
            zipFile.close();
        }
    }

    /**
     * Copies streams. The input stream is entirely consumed and closed.
     * 
     * @param in
     *        Input stream
     * @param out
     *        Output stream
     * @throws IOException
     *         In case of a reading or writing error
     */
    public static void copyStream(InputStream in, OutputStream out) throws IOException {
        in = new BufferedInputStream(in);
        try {
            out = new BufferedOutputStream(out);
            try {
                while (true) {
                    int data = in.read();
                    if (data == -1)
                        break;
                    out.write(data);
                }
            } finally {
                out.flush();
            }
        } finally {
            in.close();
        }
    }
}

Related

  1. unzip(File dest, String jar)
  2. unzip(File destDir, InputStream is)
  3. unzip(File epubfile, File destination)
  4. unzip(File file, File dest)
  5. unzip(File file, File destDir)
  6. unzip(File file, File targetDirectory)
  7. unzip(File file, File targetDirectory)
  8. unzip(File file, File toDir)
  9. unzip(File file, File toDirectory)