Here you can find the source of unzip(File zip, File dir)
Parameter | Description |
---|---|
zip | source archive to be processed |
dir | destination directory where to unzip the archive. |
Parameter | Description |
---|---|
IOException | If an I/O error occurs |
public static void unzip(File zip, File dir) throws IOException
//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(); } }