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 get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024