Unzip file to a directory
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
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 Utils {
public static void unzip(File srcFile, File destDir) throws IOException {
InputStream input = null;
OutputStream output = null;
ZipFile zipfile = new ZipFile(srcFile);
Enumeration zipEntries = zipfile.entries();
while (zipEntries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) zipEntries.nextElement();
if (entry.isDirectory()) {
new File(destDir, entry.getName()).mkdir();
continue;
}
input = new BufferedInputStream(zipfile.getInputStream(entry));
File destFile = new File(destDir, entry.getName());
FileOutputStream fos = new FileOutputStream(destFile);
output = new BufferedOutputStream(fos);
copyStreams(input, output);
input.close();
output.flush();
output.close();
}
}
private static void copyStreams(InputStream input, OutputStream output) throws IOException {
int count;
byte data[] = new byte[1024];
while ((count = input.read(data, 0, 1024)) != -1) {
output.write(data, 0, count);
}
}
}
Related examples in the same category