Java Unzip File unzip(File zipFile, File destDir)

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

Description

Unzips the InputStream to the given destination directory.

License

Open Source License

Parameter

Parameter Description
zipFile File to unzip
destDir a parameter

Exception

Parameter Description
FileNotFoundException an exception
IOException an exception

Declaration

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

Method Source Code

//package com.java2s;
/**// w w w.j  a va 2s  .c o m
 * Copyright (C) 2012 MediaShelf <http://www.yourmediashelf.com/>
 *
 * This file is part of fedora-cargo-plugin.
 *
 * fedora-cargo-plugin is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * fedora-cargo-plugin 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with fedora-cargo-plugin.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;

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

public class Main {
    /**
     * Unzips the InputStream to the given destination directory.
     * 
     * @param zipFile File to unzip
     * @param destDir
     * @throws FileNotFoundException
     * @throws IOException
     */
    public static void unzip(File zipFile, File destDir) throws FileNotFoundException, IOException {
        int BUFFER = 2048;
        BufferedOutputStream dest = null;
        ZipFile zip = new ZipFile(zipFile);
        Enumeration<? extends ZipEntry> entries = zip.entries();
        ZipEntry entry;
        while (entries.hasMoreElements()) {
            entry = entries.nextElement();
            if (entry.isDirectory()) {
                // Otherwise, empty directories do not get created
                (new File(destDir, entry.getName())).mkdirs();
            } else {
                BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry));

                File f = new File(destDir, entry.getName());
                f.getParentFile().mkdirs();
                int count;
                byte data[] = new byte[BUFFER];
                // write the files to the disk
                FileOutputStream fos = new FileOutputStream(f);
                dest = new BufferedOutputStream(fos, BUFFER);
                while ((count = is.read(data, 0, BUFFER)) != -1) {
                    dest.write(data, 0, count);
                }
                dest.flush();
                dest.close();
                is.close();
            }
        }
    }
}

Related

  1. unzip(File zipFile, File dest)
  2. unzip(File zipFile, File destDir)
  3. unzip(File zipFile, File destDir)
  4. unzip(File zipFile, File destDir)
  5. unzip(File zipFile, File destDir)
  6. unzip(File zipFile, File destination)
  7. unzip(File zipFile, File destination, IProgressMonitor monitor)
  8. unzip(File zipfile, File directory)
  9. unzip(File zipfile, File directory)