Here you can find the source of Unzip(String zipFile, String outputDirectory)
Parameter | Description |
---|---|
zipFile | zipped file to unzip |
outputDirectory | destination directory for unzipped content |
public static boolean Unzip(String zipFile, String outputDirectory)
//package com.java2s; 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.regex.Matcher; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; public class Main { /**//from w w w. j av a 2 s.co m * Unzip a zipped file (eg. kmz, zip) into given directory * * @param zipFile * zipped file to unzip * @param outputDirectory * destination directory for unzipped content * @return true if successfully unzipped */ public static boolean Unzip(String zipFile, String outputDirectory) { String outDir = outputDirectory; if (!outDir.endsWith("/") && !outDir.endsWith("\\")) outDir += File.separator; BufferedOutputStream dest = null; BufferedInputStream is = null; int BUFFER = 2048; ZipEntry entry; ZipFile zipfile; try { zipfile = new ZipFile(zipFile); Enumeration<? extends ZipEntry> e = zipfile.entries(); while (e.hasMoreElements()) { entry = e.nextElement(); if (entry.isDirectory()) { (new File(outDir + entry.getName())).mkdir(); continue; } String name; if (File.separator.equals("\\")) { name = entry.getName().replaceAll("/", Matcher.quoteReplacement(File.separator)); } else { name = entry.getName().replaceAll("\\\\", Matcher.quoteReplacement(File.separator)); } String filename = outDir + name; String filePath = filename.substring(0, filename.lastIndexOf(File.separator)); // Create directory if not existing if (!(new File(filePath)).exists()) { (new File(filePath)).mkdirs(); } is = new BufferedInputStream(zipfile.getInputStream(entry)); int count; byte data[] = new byte[BUFFER]; FileOutputStream fos = new FileOutputStream(filename); dest = new BufferedOutputStream(fos, BUFFER); while ((count = is.read(data, 0, BUFFER)) != -1) { dest.write(data, 0, count); } dest.flush(); dest.close(); is.close(); } zipfile.close(); } catch (IOException e1) { // TODO Auto-generated catch block System.err.println("Couldn't unzip file: " + zipFile); e1.printStackTrace(); return false; } return true; } }