Here you can find the source of unzipFile(ZipFile zipFile, File destinationDir)
Parameter | Description |
---|---|
zipFile | the file to unzip |
destinationDir | destination directory for the content of the zipFile |
Parameter | Description |
---|---|
FileNotFoundException | if a file could not be created |
IOException | if IO error occurs |
public static void unzipFile(ZipFile zipFile, File destinationDir) throws FileNotFoundException, IOException
//package com.java2s; //License from project: Open Source License import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; public class Main { /**//from ww w . j av a 2 s . c o m * Extracts content of the zipFile to given destination directory * * @param zipFile * the file to unzip * @param destinationDir * destination directory for the content of the zipFile * @throws FileNotFoundException * if a file could not be created * @throws IOException * if IO error occurs */ public static void unzipFile(ZipFile zipFile, File destinationDir) throws FileNotFoundException, IOException { if (!destinationDir.exists()) { destinationDir.mkdirs(); } Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); if (!entry.isDirectory()) { File file = new File(destinationDir, entry.getName()); File parentFile = file.getParentFile(); if (parentFile != null) { parentFile.mkdirs(); } InputStream inputStream = zipFile.getInputStream(entry); FileOutputStream outputStream = new FileOutputStream(file); try { copyStream(inputStream, outputStream); } finally { inputStream.close(); outputStream.close(); } } } } /** * Perform copy from input stream to output stream. * * @param is * source stream * @param os * result stream * @throws IOException */ public static void copyStream(InputStream is, OutputStream os) throws IOException { byte[] buffer = new byte[131072]; int bytesRead; while ((bytesRead = is.read(buffer)) > 0) { os.write(buffer, 0, bytesRead); } } }