Here you can find the source of unzip(File src, File dest)
Parameter | Description |
---|---|
src | The file to unzip |
dest | The destination folder |
Parameter | Description |
---|---|
IOException | when IO error occur. |
public static void unzip(File src, File dest) throws IOException
//package com.java2s; //License from project: Apache License import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; public class Main { /**//from ww w .java 2 s.co m * Size of internal buffer. */ private static final int BUFFER = 2048; /** * Unzip the the file at the specified location. * * @param src The file to unzip * @param dest The destination folder * @throws IOException when IO error occur. */ public static void unzip(File src, File dest) throws IOException { dest.mkdirs(); BufferedOutputStream out = null; BufferedInputStream is = null; try { ZipEntry entry; ZipFile zip = new ZipFile(src); for (Enumeration<? extends ZipEntry> e = zip.entries(); e.hasMoreElements();) { entry = e.nextElement(); String name = entry.getName(); // Ensure that the folder exists if (name.indexOf('/') > 0) { String folder = name.substring(0, name.lastIndexOf('/')); File dir = new File(dest, folder); if (!dir.exists()) { dir.mkdirs(); } } // Only process files if (!entry.isDirectory()) { is = new BufferedInputStream(zip.getInputStream(entry)); int count; byte[] data = new byte[BUFFER]; File f = new File(dest, name); try (FileOutputStream fos = new FileOutputStream(f)) { out = new BufferedOutputStream(fos, BUFFER); while ((count = is.read(data, 0, BUFFER)) != -1) { out.write(data, 0, count); } out.flush(); } is.close(); } } zip.close(); } finally { // If the ZIP is empty these may be null if (is != null) { is.close(); } if (out != null) { out.close(); } } } }