GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — Java ZipInputStream - reading ZIP files in Java Java ZipInputStreamlast modified July 15, 2022
Java ZipInputStream tutorial shows how to read ZIP files in Java with
Java ZipInputStream
ZIPZIP is an archive file format that supports lossless data compression. A ZIP file may contain one or more files or directories that may have been compressed. Java Archive (JAR) is built on the ZIP format. ZipInputStream constructors
ZipInputStream(InputStream in) ZipInputStream(InputStream in, Charset charset) ZipInputStream getNextEntry
The Java read ZIP exampleThe following example reads the contents of a ZIP file. com/zetcode/JavaReadZip.java
package com.zetcode;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.time.LocalDate;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class JavaReadZip {
private final static Long MILLS_IN_DAY = 86400000L;
public static void main(String[] args) throws IOException {
String fileName = "src/resources/myfile.zip";
try (FileInputStream fis = new FileInputStream(fileName);
BufferedInputStream bis = new BufferedInputStream(fis);
ZipInputStream zis = new ZipInputStream(bis)) {
ZipEntry ze;
while ((ze = zis.getNextEntry()) != null) {
System.out.format("File: %s Size: %d last modified: %d",
ze.getName(), ze.getSize(),
LocalDate.ofEpochDay(ze.getTime() / MILLS_IN_DAY));
}
}
}
}
The example reads the given ZIP file with String fileName = "src/resources/myfile.zip";
The ZIP file is located int try (FileInputStream fis = new FileInputStream(fileName);
We create a BufferedInputStream bis = new BufferedInputStream(fis);
For better performance, we pass the ZipInputStream zis = new ZipInputStream(bis)) {
A while ((ze = zis.getNextEntry()) != null) {
In a while loop, we go through the entries of the ZIP file with System.out.format("File: %s Size: %d last modified: %d",
ze.getName(), ze.getSize(),
LocalDate.ofEpochDay(ze.getTime() / MILLS_IN_DAY));
The Java decompress ZIP exampleIn the next example, we decompress a ZIP file in Java. com/zetcode/JavaUnzip.java
package com.zetcode;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class JavaUnzip {
public static void main(String args[]) throws Exception {
byte[] buffer = new byte[2048];
Path outDir = Paths.get("src/resources/output/");
String zipFileName = "src/resources/myfile.zip";
try (FileInputStream fis = new FileInputStream(zipFileName);
BufferedInputStream bis = new BufferedInputStream(fis);
ZipInputStream stream = new ZipInputStream(bis)) {
ZipEntry entry;
while ((entry = stream.getNextEntry()) != null) {
Path filePath = outDir.resolve(entry.getName());
try (FileOutputStream fos = new FileOutputStream(filePath.toFile());
BufferedOutputStream bos = new BufferedOutputStream(fos, buffer.length)) {
int len;
while ((len = stream.read(buffer)) > 0) {
bos.write(buffer, 0, len);
}
}
}
}
}
}
The example uses Path outDir = Paths.get("src/resources/output/");
This is the directory where we extract the contents of the ZIP file. while ((entry = stream.getNextEntry()) != null) {
In the first while loop, we go through the entries of the ZIP file. while ((len = stream.read(buffer)) > 0) {
bos.write(buffer, 0, len);
}
In the second while loop, we read the entries and write them to the output stream. SourceJava ZipInputStream - language reference
In this article we have presented the Java AuthorMy name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming. List all Java tutorials. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |