Java Unzip File unzip(File zip, File dir)

Here you can find the source of unzip(File zip, File dir)

Description

A quick and dirty method to unzip an archive into a directory.

License

Open Source License

Parameter

Parameter Description
zip source archive to be processed
dir destination directory where to unzip the archive.

Exception

Parameter Description
IOException If an I/O error occurs

Declaration

public static void unzip(File zip, File dir) throws IOException 

Method Source Code


//package com.java2s;
/*//from w w  w  .ja v  a2 s.  c o  m
 * Copyright 2004-2009 Luciano Vernaschi
 *
 * This file is part of MeshCMS.
 *
 * MeshCMS 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.
 *
 * MeshCMS 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 MeshCMS.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.BufferedInputStream;

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

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

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

public class Main {
    /**
     * A standard size for buffers.
     */
    public static final int BUFFER_SIZE = 2048;

    /**
     * A quick and dirty method to unzip an archive into a directory.
     *
     * @param zip source archive to be processed
     * @param dir destination directory where to unzip the archive.
     *
     * @throws IOException If an I/O error occurs
     */
    public static void unzip(File zip, File dir) throws IOException {
        dir.mkdirs();
        InputStream in = new BufferedInputStream(new FileInputStream(zip));
        ZipInputStream zin = new ZipInputStream(in);
        ZipEntry e;

        while ((e = zin.getNextEntry()) != null) {
            File f = new File(dir, e.getName());

            if (e.isDirectory()) {
                f.mkdirs();
            } else {
                f.getParentFile().mkdirs();
                FileOutputStream out = new FileOutputStream(f);
                byte[] b = new byte[BUFFER_SIZE];
                int len;

                while ((len = zin.read(b)) != -1) {
                    out.write(b, 0, len);
                }

                out.close();
            }
        }

        zin.close();
    }
}

Related

  1. unzip(File target)
  2. unzip(File targetZip, File dirToExtract)
  3. unzip(File zip)
  4. unZip(File zip, File dest)
  5. unzip(File zip, File destination)
  6. unzip(File zip, File directory)
  7. unzip(File zip, File extractTo)
  8. unzip(File zip, File extractTo)
  9. unzip(File zip, File location)