Here you can find the source of unzip(File file, File toDir)
public static void unzip(File file, File toDir) throws Throwable
//package com.java2s; //License from project: Open Source License import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.Collection; import java.util.Enumeration; public class Main { public static final int BUFFER_SIZE = 2048; public static void unzip(File file, File toDir) throws Throwable { unzip(file, toDir, null);// www . j a v a2s . c om } public static void unzip(File file, File toDir, Collection<File> files) throws Throwable { unzip(file, toDir, files, false); } /** * Extract zip file to the specified directory also save the extracted files * into the specified Collection. (Using java.util.zip). The order of * unzipping does preserve the order in which files were packed in to the * zip archive. * * @param file * the zip file to extract. * @param toDir * the destination directory. * @param files * the collection of the extracted files. Set to null if no * references to the extracted files need to be kept. * @param stream * set to true to use ZipInputStream instead of ZipFile. See <a * href= * "http://java.sun.com/developer/technicalArticles/Programming/compression/" * >Detail</a> * @throws Throwable */ public static void unzip(File file, File toDir, Collection<File> files, boolean stream) throws Throwable { byte[] buffer = new byte[BUFFER_SIZE]; if (stream) { java.util.zip.ZipInputStream zis = new java.util.zip.ZipInputStream( new BufferedInputStream(new FileInputStream(file))); java.util.zip.ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { File destFile = new File(toDir.getAbsolutePath() + "/" + entry.getName()); if (entry.isDirectory()) { destFile.mkdirs(); } else { File parentDir = destFile.getParentFile(); if (!parentDir.exists()) { parentDir.mkdirs(); } BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(destFile), BUFFER_SIZE); try { int count; while ((count = zis.read(buffer, 0, BUFFER_SIZE)) != -1) { os.write(buffer, 0, count); } os.flush(); } finally { os.close(); } } if (files != null) { files.add(destFile); } } zis.close(); } else { java.util.zip.ZipFile zipFile = new java.util.zip.ZipFile(file); Enumeration<?> entries = zipFile.entries(); while (entries.hasMoreElements()) { java.util.zip.ZipEntry entry = (java.util.zip.ZipEntry) entries.nextElement(); File destFile = new File(toDir.getAbsolutePath() + "/" + entry.getName()); if (entry.isDirectory()) { destFile.mkdirs(); } else { File parentDir = destFile.getParentFile(); if (!parentDir.exists()) { parentDir.mkdirs(); } BufferedInputStream is = new BufferedInputStream(zipFile.getInputStream(entry), BUFFER_SIZE); BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(destFile), BUFFER_SIZE); try { int count; while ((count = is.read(buffer, 0, BUFFER_SIZE)) != -1) { os.write(buffer, 0, count); } os.flush(); } finally { os.close(); is.close(); } } if (files != null) { files.add(destFile); } } } } }