- Java ZipInputStream
- Java ZipInputStream
- ZIP
- ZipInputStream constructors
- ZipInputStream getNextEntry
- Java read ZIP example
- Java decompress ZIP example
- Author
- Java read from zip file
- Field Summary
- Fields inherited from class java.util.zip.InflaterInputStream
- Fields inherited from class java.io.FilterInputStream
- Constructor Summary
- Method Summary
- Methods inherited from class java.util.zip.InflaterInputStream
- Methods inherited from class java.io.FilterInputStream
- Methods inherited from class java.lang.Object
- Field Detail
- LOCSIG
- EXTSIG
- CENSIG
- ENDSIG
- LOCHDR
- EXTHDR
- CENHDR
- ENDHDR
- LOCVER
- LOCFLG
- LOCHOW
- LOCTIM
- LOCCRC
- LOCSIZ
- LOCLEN
- LOCNAM
- LOCEXT
- EXTCRC
- EXTSIZ
- EXTLEN
- CENVEM
- CENVER
- CENFLG
- CENHOW
- CENTIM
- CENCRC
- CENSIZ
- CENLEN
- CENNAM
- CENEXT
- CENCOM
- CENDSK
- CENATT
- CENATX
- CENOFF
- ENDSUB
- ENDTOT
- ENDSIZ
- ENDOFF
- ENDCOM
- Constructor Detail
- ZipInputStream
- ZipInputStream
- Method Detail
- getNextEntry
- closeEntry
- available
- read
- skip
- close
- createZipEntry
- Class ZipFile
- Field Summary
- Constructor Summary
- Method Summary
- Methods declared in class java.lang.Object
- Field Details
- OPEN_READ
- OPEN_DELETE
- LOCSIG
- EXTSIG
- CENSIG
- ENDSIG
- LOCHDR
- EXTHDR
- CENHDR
- ENDHDR
- LOCVER
- LOCFLG
- LOCHOW
- LOCTIM
- LOCCRC
- LOCSIZ
- LOCLEN
- LOCNAM
- LOCEXT
- EXTCRC
- EXTSIZ
- EXTLEN
- CENVEM
- CENVER
- CENFLG
- CENHOW
- CENTIM
- CENCRC
- CENSIZ
- CENLEN
- CENNAM
- CENEXT
- CENCOM
- CENDSK
- CENATT
- CENATX
- CENOFF
- ENDSUB
- ENDTOT
- ENDSIZ
- ENDOFF
- ENDCOM
- Constructor Details
- ZipFile
- ZipFile
- ZipFile
- ZipFile
- ZipFile
- ZipFile
- Method Details
- getComment
- getEntry
- getInputStream
- getName
- entries
- stream
- size
- close
- Class ZipInputStream
- Field Summary
- Fields declared in class java.util.zip.InflaterInputStream
- Fields declared in class java.io.FilterInputStream
- Constructor Summary
- Method Summary
- Methods declared in class java.util.zip.InflaterInputStream
- Methods declared in class java.io.FilterInputStream
- Methods declared in class java.io.InputStream
- Methods declared in class java.lang.Object
- Field Details
- LOCSIG
- EXTSIG
- CENSIG
- ENDSIG
- LOCHDR
- EXTHDR
- CENHDR
- ENDHDR
- LOCVER
- LOCFLG
- LOCHOW
- LOCTIM
- LOCCRC
- LOCSIZ
- LOCLEN
- LOCNAM
- LOCEXT
- EXTCRC
- EXTSIZ
- EXTLEN
- CENVEM
- CENVER
- CENFLG
- CENHOW
- CENTIM
- CENCRC
- CENSIZ
- CENLEN
- CENNAM
- CENEXT
- CENCOM
- CENDSK
- CENATT
- CENATX
- CENOFF
- ENDSUB
- ENDTOT
- ENDSIZ
- ENDOFF
- ENDCOM
- Constructor Details
- ZipInputStream
- ZipInputStream
- Method Details
- getNextEntry
- closeEntry
- available
- read
- skip
- close
- createZipEntry
Java ZipInputStream
Java ZipInputStream tutorial shows how to read ZIP files in Java with ZipInputStream .
Java ZipInputStream
ZipInputStream is a Java class that implements an input stream filter for reading files in the ZIP file format. It has support for both compressed and uncompressed entries.
ZIP
ZIP 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 has the following constructors:
ZipInputStream(InputStream in) ZipInputStream(InputStream in, Charset charset)
ZipInputStream getNextEntry
The ZipInputStream’s getNextEntry reads the next ZIP file entry and positions the stream at the beginning of the entry data.
Java read ZIP example
The following example reads the contents of a ZIP file.
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 ZipInputStream and prints its contents to the terminal. We print the file names, their size, and the last modification time.
String fileName = "src/resources/myfile.zip";
The ZIP file is located int src/resources/ directory.
try (FileInputStream fis = new FileInputStream(fileName);
We create a FileInputStream from the file. FileInputStream is used for reading streams of raw bytes.
BufferedInputStream bis = new BufferedInputStream(fis);
For better performance, we pass the FileInputStream into the BufferedInputStream .
ZipInputStream zis = new ZipInputStream(bis))A ZipInputStream is created from the buffered FileInputStream . The try-with-resources closes the streams when they are not needed anymore.
while ((ze = zis.getNextEntry()) != null)In a while loop, we go through the entries of the ZIP file with getNextEntry method. It returns null if there are no more entries.
System.out.format("File: %s Size: %d last modified: %d", ze.getName(), ze.getSize(), LocalDate.ofEpochDay(ze.getTime() / MILLS_IN_DAY));The getName returns the name of the entry, the getSize returns the uncompressed size of the entry, and the getTime returns the last modification time of the entry.
Java decompress ZIP example
In the next example, we decompress a ZIP file in 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 ZipInputStream to read the contents of the given ZIP file and FileOutputStream and BufferedOutputStream to write the contents into a directory.
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)In the second while loop, we read the entries and write them to the output stream.
In this article we have presented the Java ZipInputStream class. We have created two examples to read a ZIP file and to decompress a ZIP file.
Author
My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.
Java read from zip file
This class implements an input stream filter for reading files in the ZIP file format. Includes support for both compressed and uncompressed entries.
Field Summary
Fields inherited from class java.util.zip.InflaterInputStream
Fields inherited from class java.io.FilterInputStream
Constructor Summary
Method Summary
Methods inherited from class java.util.zip.InflaterInputStream
Methods inherited from class java.io.FilterInputStream
Methods inherited from class java.lang.Object
Field Detail
LOCSIG
public static final long LOCSIGEXTSIG
public static final long EXTSIGCENSIG
public static final long CENSIGENDSIG
public static final long ENDSIGLOCHDR
public static final int LOCHDREXTHDR
public static final int EXTHDRCENHDR
public static final int CENHDRENDHDR
public static final int ENDHDRLOCVER
public static final int LOCVERLOCFLG
public static final int LOCFLGLOCHOW
public static final int LOCHOWLOCTIM
public static final int LOCTIMLOCCRC
public static final int LOCCRCLOCSIZ
public static final int LOCSIZLOCLEN
public static final int LOCLENLOCNAM
public static final int LOCNAMLOCEXT
public static final int LOCEXTEXTCRC
public static final int EXTCRCEXTSIZ
public static final int EXTSIZEXTLEN
public static final int EXTLENCENVEM
public static final int CENVEMCENVER
public static final int CENVERCENFLG
public static final int CENFLGCENHOW
public static final int CENHOWCENTIM
public static final int CENTIMCENCRC
public static final int CENCRCCENSIZ
public static final int CENSIZCENLEN
public static final int CENLENCENNAM
public static final int CENNAMCENEXT
public static final int CENEXTCENCOM
public static final int CENCOMCENDSK
public static final int CENDSKCENATT
public static final int CENATTCENATX
public static final int CENATXCENOFF
public static final int CENOFFENDSUB
public static final int ENDSUBENDTOT
public static final int ENDTOTENDSIZ
public static final int ENDSIZENDOFF
public static final int ENDOFFENDCOM
public static final int ENDCOMConstructor Detail
ZipInputStream
ZipInputStream
Method Detail
getNextEntry
public ZipEntry getNextEntry() throws IOExceptioncloseEntry
available
Returns 0 after EOF has reached for the current entry data, otherwise always return 1. Programs should not count on this method to return the actual number of bytes that could be read without blocking.
read
Reads from the current ZIP entry into an array of bytes. If len is not zero, the method blocks until some input is available; otherwise, no bytes are read and 0 is returned.
skip
close
createZipEntry
protected ZipEntry createZipEntry(String name)Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2023, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.Class ZipFile
Unless otherwise noted, passing a null argument to a constructor or method in this class will cause a NullPointerException to be thrown.
Field Summary
Constructor Summary
Method Summary
Methods declared in class java.lang.Object
Field Details
OPEN_READ
OPEN_DELETE
Mode flag to open a zip file and mark it for deletion. The file will be deleted some time between the moment that it is opened and the moment that it is closed, but its contents will remain accessible via the ZipFile object until either the close method is invoked or the virtual machine exits.
LOCSIG
EXTSIG
CENSIG
ENDSIG
LOCHDR
EXTHDR
CENHDR
ENDHDR
LOCVER
LOCFLG
LOCHOW
LOCTIM
LOCCRC
LOCSIZ
LOCLEN
LOCNAM
LOCEXT
EXTCRC
EXTSIZ
EXTLEN
CENVEM
CENVER
CENFLG
CENHOW
CENTIM
CENCRC
CENSIZ
CENLEN
CENNAM
CENEXT
CENCOM
CENDSK
CENATT
CENATX
CENOFF
ENDSUB
ENDTOT
ENDSIZ
ENDOFF
ENDCOM
Constructor Details
ZipFile
Opens a zip file for reading. First, if there is a security manager, its checkRead method is called with the name argument as its argument to ensure the read is allowed. The UTF-8 charset is used to decode the entry names and comments.
ZipFile
Opens a new ZipFile to read from the specified File object in the specified mode. The mode argument must be either OPEN_READ or OPEN_READ | OPEN_DELETE . First, if there is a security manager, its checkRead method is called with the name argument as its argument to ensure the read is allowed. The UTF-8 charset is used to decode the entry names and comments
ZipFile
Opens a ZIP file for reading given the specified File object. The UTF-8 charset is used to decode the entry names and comments.
ZipFile
Opens a new ZipFile to read from the specified File object in the specified mode. The mode argument must be either OPEN_READ or OPEN_READ | OPEN_DELETE . First, if there is a security manager, its checkRead method is called with the name argument as its argument to ensure the read is allowed.
ZipFile
Opens a zip file for reading. First, if there is a security manager, its checkRead method is called with the name argument as its argument to ensure the read is allowed.
ZipFile
Method Details
getComment
getEntry
getInputStream
Returns an input stream for reading the contents of the specified zip file entry. Closing this ZIP file will, in turn, close all input streams that have been returned by invocations of this method.
getName
entries
stream
Returns an ordered Stream over the ZIP file entries. Entries appear in the Stream in the order they appear in the central directory of the ZIP file.
size
close
Closes the ZIP file. Closing this ZIP file will close all of the input streams previously returned by invocations of the getInputStream method.
Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples. Other versions.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.Class ZipInputStream
This class implements an input stream filter for reading files in the ZIP file format. Includes support for both compressed and uncompressed entries.
Field Summary
Fields declared in class java.util.zip.InflaterInputStream
Fields declared in class java.io.FilterInputStream
Constructor Summary
Method Summary
Methods declared in class java.util.zip.InflaterInputStream
Methods declared in class java.io.FilterInputStream
Methods declared in class java.io.InputStream
Methods declared in class java.lang.Object
Field Details
LOCSIG
EXTSIG
CENSIG
ENDSIG
LOCHDR
EXTHDR
CENHDR
ENDHDR
LOCVER
LOCFLG
LOCHOW
LOCTIM
LOCCRC
LOCSIZ
LOCLEN
LOCNAM
LOCEXT
EXTCRC
EXTSIZ
EXTLEN
CENVEM
CENVER
CENFLG
CENHOW
CENTIM
CENCRC
CENSIZ
CENLEN
CENNAM
CENEXT
CENCOM
CENDSK
CENATT
CENATX
CENOFF
ENDSUB
ENDTOT
ENDSIZ
ENDOFF
ENDCOM
Constructor Details
ZipInputStream
ZipInputStream
Method Details
getNextEntry
closeEntry
available
Returns 0 after EOF has reached for the current entry data, otherwise always return 1. Programs should not count on this method to return the actual number of bytes that could be read without blocking.
read
Reads from the current ZIP entry into an array of bytes. If len is not zero, the method blocks until some input is available; otherwise, no bytes are read and 0 is returned.
skip
close
createZipEntry
Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples. Other versions.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.