How do I decompress a zip file using ZipFile?
Category: java.util.zip, viewed: 6032 time(s).
In this example we use the ZipFile class to decompress and extract a zip file.
package org.kodejava.example.util.zip;
import java.util.zip.ZipFile;
import java.util.zip.ZipEntry;
import java.util.Enumeration;
import java.io.*;
public class ZipFileUnzipDemo {
public static void main(String[] args) {
String zipname = "data.zip";
try {
ZipFile zipFile = new ZipFile(zipname);
Enumeration enumeration = zipFile.entries();
while (enumeration.hasMoreElements()) {
ZipEntry zipEntry = (ZipEntry) enumeration.nextElement();
System.out.println("Unzipping: " + zipEntry.getName());
BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(zipEntry));
int size;
byte[] buffer = new byte[2048];
FileOutputStream fos = new FileOutputStream(zipEntry.getName());
BufferedOutputStream bos = new BufferedOutputStream(fos, buffer.length);
while ((size = bis.read(buffer, 0, buffer.length)) != -1) {
bos.write(buffer, 0, size);
}
bos.flush();
bos.close();
fos.close();
bis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Download Hundreds of Complimentary Industry Resources
Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more;
all available at no cost to you. With more than 600 complimentary offers, you'll find
plenty of titles to suit your professional interests and needs.
Click Here and Sign up today!