Java Unzip File unzip(File zipFile, File destDir)

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

Description

Unzip an archive into the directory destDir.

License

Open Source License

Parameter

Parameter Description
zipFile a parameter
destDir a parameter

Exception

Parameter Description
IOException an exception

Declaration

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

Method Source Code

//package com.java2s;
/**//  www.j  a  va  2s  .c  o  m
 * OrbisGIS is a GIS application dedicated to scientific spatial simulation.
 * This cross-platform GIS is developed at French IRSTV institute and is able to
 * manipulate and create vector and raster spatial information.
 *
 * OrbisGIS is distributed under GPL 3 license. It is produced by the "Atelier SIG"
 * team of the IRSTV Institute <http://www.irstv.fr/> CNRS FR 2488.
 *
 * Copyright (C) 2007-2012 IRSTV (FR CNRS 2488)
 *
 * This file is part of OrbisGIS.
 *
 * OrbisGIS is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * OrbisGIS 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
 * OrbisGIS. If not, see <http://www.gnu.org/licenses/>.
 *
 * For more information, please consult: <http://www.orbisgis.org/>
 * or contact directly:
 * info_at_ orbisgis.org
 */

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import java.io.IOException;

import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class Main {
    private static final int BUF_SIZE = 1024 * 64;

    /**
     * Unzip an archive into the directory destDir.
     * @param zipFile
     * @param destDir
     * @throws IOException
     */
    public static void unzip(File zipFile, File destDir) throws IOException {
        ZipInputStream zis = null;

        try {
            FileInputStream fis = new FileInputStream(zipFile);
            zis = new ZipInputStream(new BufferedInputStream(fis));
            ZipEntry entry = zis.getNextEntry();
            while (entry != null) {
                byte data[] = new byte[BUF_SIZE];
                // write the files to the disk
                File newFile = new File(destDir, entry.getName());
                File parentFile = newFile.getParentFile();
                if (!parentFile.exists() && !parentFile.mkdirs()) {
                    throw new IOException("Cannot create directory:" + parentFile);
                }
                if (!entry.isDirectory()) {
                    BufferedOutputStream dest = null;
                    try {
                        FileOutputStream fos = new FileOutputStream(newFile);
                        dest = new BufferedOutputStream(fos, BUF_SIZE);
                        int count = zis.read(data, 0, BUF_SIZE);
                        while (count != -1) {
                            dest.write(data, 0, count);
                            count = zis.read(data, 0, BUF_SIZE);
                        }
                        dest.flush();
                    } finally {
                        if (dest != null) {
                            dest.close();
                        }
                    }
                }
                entry = zis.getNextEntry();
            }
        } finally {
            if (zis != null) {
                zis.close();
            }
        }
    }
}

Related

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