In this example we use the java.util.zip.ZipFile
class to decompress and extract a zip file.
package org.kodejava.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) throws Exception {
String zipName = "data.zip";
ZipFile zip = new ZipFile(zipName);
Enumeration<? extends ZipEntry> enumeration = zip.entries();
while (enumeration.hasMoreElements()) {
ZipEntry zipEntry = enumeration.nextElement();
System.out.println("Unzipping: " + zipEntry.getName());
int size;
byte[] buffer = new byte[2048];
try (BufferedInputStream bis =
new BufferedInputStream(zip.getInputStream(zipEntry));
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();
}
}
}
}
Latest posts by Wayan (see all)
- How do I handle cookies using Jakarta Servlet API? - April 19, 2025
- How do I set response headers with HttpServletResponse? - April 18, 2025
- How do I apply gain and balance using FloatControl? - April 18, 2025