Here you can find the source of unzip(File zip, File location)
Parameter | Description |
---|---|
zip | The zip compressed file |
location | The location to unzip into |
Parameter | Description |
---|---|
IOException | an exception |
public static void unzip(File zip, File location) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class Main { /**// ww w .j av a2s . c o m * Unzips a zip compressed file to a specific directory * * @param zip * The zip compressed file * @param location * The location to unzip into * @throws IOException */ public static void unzip(File zip, File location) throws IOException { FileInputStream zipFileInput = new FileInputStream(zip.getAbsoluteFile()); ZipInputStream zipInput = new ZipInputStream(zipFileInput); ZipEntry entry = null; String baseDir = null; if (location == null) { baseDir = zip.getParentFile().getAbsolutePath(); } else { baseDir = location.getAbsolutePath(); } while ((entry = zipInput.getNextEntry()) != null) { String name = entry.getName(); File outFile = new File(baseDir + File.separator + name); if (entry.isDirectory()) { outFile.mkdirs(); } else { if (!outFile.getParentFile().exists()) { outFile.getParentFile().mkdirs(); } outFile.createNewFile(); BufferedOutputStream fileOut = new BufferedOutputStream(new FileOutputStream(outFile)); copyStreams(zipInput, fileOut); fileOut.flush(); fileOut.close(); } } zipInput.close(); } /** * Copies the contents of an input stream into an output stream * * @param input * @param output * @throws IOException */ private static void copyStreams(InputStream input, OutputStream output) throws IOException { byte[] temp = new byte[8192]; int read = -1; while ((read = input.read(temp)) != -1) { output.write(temp, 0, read); } } }