Java Unzip ZipFile unzip(ZipFile zipFile, File destDir)

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

Description

Unzip.

License

Mozilla Public License

Parameter

Parameter Description
zipFile the zip file
destDir the dest dir

Declaration

public static void unzip(ZipFile zipFile, File destDir) 

Method Source Code


//package com.java2s;
/* SpagoBI, the Open Source Business Intelligence suite
    // w w w  .j a  va  2  s.  co m
 * Copyright (C) 2012 Engineering Ingegneria Informatica S.p.A. - SpagoBI Competency Center
 * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0, without the "Incompatible With Secondary Licenses" notice. 
 * If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. */

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 {
    /**
     * Unzip.
     * 
     * @param zipFile the zip file
     * @param destDir the dest dir
     */
    public static void unzip(ZipFile zipFile, File destDir) {

        Enumeration entries;

        try {

            entries = zipFile.entries();

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

                if (!entry.isDirectory()) {
                    File destFile = new File(destDir, entry.getName());
                    File destFileDir = destFile.getParentFile();
                    if (!destFileDir.exists()) {
                        System.err.println("Extracting directory: "
                                + entry.getName().substring(0, entry.getName().lastIndexOf('/')));
                        destFileDir.mkdirs();
                    }

                    System.err.println("Extracting file: " + entry.getName());
                    copyInputStream(zipFile.getInputStream(entry),
                            new BufferedOutputStream(new FileOutputStream(new File(destDir, entry.getName()))));
                }
            }

            zipFile.close();
        } catch (IOException ioe) {
            System.err.println("Unhandled exception:");
            ioe.printStackTrace();
            return;
        }
    }

    /**
     * Copy input stream.
     * 
     * @param in the in
     * @param out the out
     * 
     * @throws IOException Signals that an I/O exception has occurred.
     */
    public static final void copyInputStream(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[1024];
        int len;

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

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

Related

  1. unzip(final File zip, final File patchDir)
  2. unzip(final File zipFile, final String suffix)
  3. unzip(ZipEntry entry, ZipFile zipfile, File explodedDir)
  4. unzip(ZipFile zip, File fileDir)
  5. unzip(ZipFile zipFile, File dest)
  6. unzip(ZipFile zipFile, File destDirectory)
  7. unzip(ZipFile zipFile, File dstDir)
  8. unZip(ZipFile zipFile, File outputDir)