Java Unzip File unzip(File file, File targetDirectory)

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

Description

Unzip a file to a specified directory

License

Open Source License

Declaration

public static void unzip(File file, File targetDirectory) throws Exception 

Method Source Code

//package com.java2s;
/**/*from   w ww .j a va 2 s .  c o  m*/
 * e-Science Central
 * Copyright (C) 2008-2013 School of Computing Science, Newcastle University
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation at:
 * http://www.gnu.org/licenses/gpl-2.0.html
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301, USA.
 */

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

public class Main {
    /** Unzip a file to a specified directory */
    public static void unzip(File file, File targetDirectory) throws Exception {
        Enumeration<? extends ZipEntry> entries;
        ZipFile zipFile = new ZipFile(file);

        entries = zipFile.entries();

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

            if (!entry.getName().startsWith(".")) {
                if (entry.isDirectory()) {
                    // Assume directories are stored parents first then children.
                    (new File(targetDirectory, entry.getName())).mkdirs();
                    continue;
                }

                InputStream inStream = zipFile.getInputStream(entry);
                BufferedOutputStream outStream = null;
                try {
                    outStream = new BufferedOutputStream(
                            new FileOutputStream(new File(targetDirectory, entry.getName())));
                    copyInputStream(inStream, outStream);
                } catch (Exception e) {
                    System.out.println("Zip error: " + e.getMessage());
                } finally {
                    inStream.close();
                    if (outStream != null) {
                        outStream.close();
                    }
                }
            }
        }
        zipFile.close();
    }

    /** Copy the data from one stream to another */
    public static void copyInputStream(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[4096];
        int len;

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

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

    /** Copy the data from one stream to another */
    public static void copyInputStream(InputStream in, OutputStream out, int sizeLimit) throws IOException {
        byte[] buffer = new byte[4096];
        int len;
        int totalBytes = 0;
        while ((len = in.read(buffer)) >= 0 && totalBytes < sizeLimit) {
            out.write(buffer, 0, len);
            totalBytes = totalBytes + len;
        }

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

Related

  1. unzip(File epubfile, File destination)
  2. unzip(File file, File dest)
  3. unzip(File file, File destDir)
  4. unzip(File file, File directory)
  5. unzip(File file, File targetDirectory)
  6. unzip(File file, File toDir)
  7. unzip(File file, File toDirectory)
  8. unzip(File file, String destination, String container)
  9. unZip(File file, String outPath, String zipFileName)