Here you can find the source of unzip(File zipFile, File destDir)
Parameter | Description |
---|---|
zipFile | a parameter |
Parameter | Description |
---|---|
IOException | an exception |
public static void unzip(File zipFile, File destDir) throws IOException
//package com.java2s; //License from project: Open Source License 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 { /**/* www . j a v a 2 s.c om*/ * Unzip a zip file to a destination. * The destination must exist before calling this method! * @param zipFile * @throws IOException */ public static void unzip(File zipFile, File destDir) throws IOException { unzip(zipFile, destDir, new byte[1024 * 32]); } public static void unzip(File zipFile, File destDir, byte[] buffer) throws IOException { if (!destDir.exists()) { throw new IOException("Destination directory " + destDir.getAbsolutePath() + " doesn't exist"); } ZipInputStream zis = null; FileOutputStream fos = null; try { zis = new ZipInputStream(new FileInputStream(zipFile)); ZipEntry entry = zis.getNextEntry(); while (entry != null) { File extractTo = new File(destDir, entry.getName()); if (entry.isDirectory()) { extractTo.mkdir(); entry = zis.getNextEntry(); continue; } else { extractTo.createNewFile(); } fos = new FileOutputStream(extractTo); int len; while ((len = zis.read(buffer)) > 0) { fos.write(buffer, 0, len); } fos.close(); entry = zis.getNextEntry(); } } finally { zis.closeEntry(); zis.close(); fos.close(); } } public static void mkdir(File f) { if (!f.exists()) { f.mkdir(); } } }