Here you can find the source of unzip(File archive, File output)
Parameter | Description |
---|---|
archive | a ZIP archive file |
output | the directory where the ZIP archive file has to be unzipped |
public static void unzip(File archive, File output)
//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 { /**//w w w .j av a 2 s .co m * Unzip a ZIP archive into the specified output directory * @param archive a ZIP archive file * @param output the directory where the ZIP archive file has to be unzipped */ public static void unzip(File archive, File output) { try { if (!output.exists()) { output.mkdir(); } ZipInputStream zis = new ZipInputStream(new FileInputStream(archive)); ZipEntry ze = zis.getNextEntry(); while (ze != null) { String fileName = ze.getName(); File newFile = new File(output, fileName); // create all non-existing folders to avoid FileNotFoundException new File(newFile.getParent()).mkdirs(); FileOutputStream fos = new FileOutputStream(newFile); byte[] buffer = new byte[1024]; int len; while ((len = zis.read(buffer)) > 0) { fos.write(buffer, 0, len); } fos.close(); ze = zis.getNextEntry(); } zis.closeEntry(); zis.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } }