Here you can find the source of unzip(File zipFile, File destDir)
Parameter | Description |
---|---|
zipFile | zip file |
destDir | destination directory |
Parameter | Description |
---|---|
IOException | an exception |
public static void unzip(File zipFile, File destDir) throws IOException
//package com.java2s; //License from project: Apache License import java.io.*; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; public class Main { /**/*from www .j ava2 s . co m*/ * Unpacks a zip file to the target directory. * * @param zipFile * zip file * @param destDir * destination directory * @throws IOException */ public static void unzip(File zipFile, File destDir) throws IOException { ZipFile zip = new ZipFile(zipFile); try { Enumeration<?> en = zip.entries(); int bufSize = 8196; while (en.hasMoreElements()) { ZipEntry entry = (ZipEntry) en.nextElement(); File file = (destDir != null) ? new File(destDir, entry.getName()) : new File(entry.getName()); if (entry.isDirectory()) { if (!file.mkdirs()) { if (file.isDirectory() == false) { throw new IOException("Error creating directory: " + file); } } } else { unzi(zip, bufSize, entry, file); } } } finally { zip.close(); } } private static void unzi(ZipFile zip, int bufSize, ZipEntry entry, File file) throws IOException, FileNotFoundException { File parent = file.getParentFile(); if (parent != null && !parent.exists()) { if (!parent.mkdirs()) { if (file.isDirectory() == false) { throw new IOException("Error creating directory: " + parent); } } } InputStream in = zip.getInputStream(entry); try { OutputStream out = new BufferedOutputStream(new FileOutputStream(file), bufSize); try { copyPipe(in, out, bufSize); } finally { out.close(); } } finally { in.close(); } } private static void copyPipe(InputStream in, OutputStream out, int bufSizeHint) throws IOException { int read; byte[] buf = new byte[bufSizeHint]; while ((read = in.read(buf, 0, bufSizeHint)) >= 0) { out.write(buf, 0, read); } out.flush(); } }