Here you can find the source of unzip(File file, File toDirectory)
Parameter | Description |
---|---|
file | The file to unzip |
toDirectory | The directory to unzip to |
Parameter | Description |
---|---|
IOException | an exception |
public static void unzip(File file, File toDirectory) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.*; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class Main { /**/* www . j a va 2 s. com*/ * Unzips a given file to the set directory * @param file The file to unzip * @param toDirectory The directory to unzip to * @throws IOException */ public static void unzip(File file, File toDirectory) throws IOException { byte[] buffer = new byte[1024]; if (!toDirectory.exists()) toDirectory.mkdir(); ZipInputStream zips = new ZipInputStream(new FileInputStream(file)); ZipEntry zip = zips.getNextEntry(); while (zip != null) { String fileName = zip.getName(); File newFile = new File(toDirectory.getPath() + File.separator + fileName); new File(newFile.getParent()).mkdirs(); FileOutputStream fos = new FileOutputStream(newFile); int len; while ((len = zips.read(buffer)) > 0) { fos.write(buffer, 0, len); } fos.close(); zip = zips.getNextEntry(); } zips.closeEntry(); zips.close(); } }