Class ZipInputStream
- java.lang.Object
-
- java.io.InputStream
-
- java.io.FilterInputStream
-
- java.util.zip.InflaterInputStream
-
- java.util.zip.ZipInputStream
-
- All Implemented Interfaces:
Closeable,AutoCloseable
- Direct Known Subclasses:
JarInputStream
public class ZipInputStream extends InflaterInputStream
Used to read (decompress) the data from zip files.A zip file (or "archive") is a collection of (possibly) compressed files. When reading from a
ZipInputStream, you callgetNextEntry()which returns aZipEntryof metadata corresponding to the userdata that follows. When you appear to have hit the end of this stream (which is really just the end of the current entry's userdata), callgetNextEntryagain. When it returns null, there are no more entries in the input file.Although
InflaterInputStreamcan only read compressed zip entries, this class can read non-compressed entries as well.Use
ZipFileif you need random access to entries by name, but use this class if you just want to iterate over all entries.Example
Using
ZipInputStreamis a little more complicated thanGZIPInputStreambecause zip files are containers that can contain multiple files. This code pulls all the files out of a zip file, similar to theunzip(1)utility.InputStream is = ... ZipInputStream zis = new ZipInputStream(new BufferedInputStream(is)); try { ZipEntry ze; while ((ze = zis.getNextEntry()) != null) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int count; while ((count = zis.read(buffer)) != -1) { baos.write(buffer, 0, count); } String filename = ze.getName(); byte[] bytes = baos.toByteArray(); // do something with 'filename' and 'bytes'... } } finally { zis.close(); }
-
-
Field Summary
Fields Modifier and Type Field Description static intCENATTstatic intCENATXstatic intCENCOMstatic intCENCRCstatic intCENDSKstatic intCENEXTstatic intCENFLGstatic intCENHDRstatic intCENHOWstatic intCENLENstatic intCENNAMstatic intCENOFFstatic longCENSIGstatic intCENSIZstatic intCENTIMstatic intCENVEMstatic intCENVERstatic intENDCOMstatic intENDHDRstatic intENDOFFstatic longENDSIGstatic intENDSIZstatic intENDSUBstatic intENDTOTstatic intEXTCRCstatic intEXTHDRstatic intEXTLENstatic longEXTSIGstatic intEXTSIZstatic intLOCCRCstatic intLOCEXTstatic intLOCFLGstatic intLOCHDRstatic intLOCHOWstatic intLOCLENstatic intLOCNAMstatic longLOCSIGstatic intLOCSIZstatic intLOCTIMstatic intLOCVER-
Fields inherited from class java.util.zip.InflaterInputStream
buf, inf, len
-
Fields inherited from class java.io.FilterInputStream
in
-
-
Constructor Summary
Constructors Constructor Description ZipInputStream(InputStream stream)Constructs a newZipInputStreamto read zip entries from the given input stream.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intavailable()Returns 0 when when this stream has exhausted its input; and 1 otherwise.voidclose()Closes thisZipInputStream.voidcloseEntry()Closes the current zip entry and prepares to read the next entry.protected ZipEntrycreateZipEntry(String name)creates aZipEntrywith the given name.ZipEntrygetNextEntry()Returns the next entry from thisZipInputStreamornullif no more entries are present.intread(byte[] buffer, int byteOffset, int byteCount)Reads up tobyteCountuncompressed bytes into the buffer starting atbyteOffset.-
Methods inherited from class java.util.zip.InflaterInputStream
fill, mark, markSupported, read, reset, skip
-
Methods inherited from class java.io.InputStream
read
-
-
-
-
Field Detail
-
LOCSIG
public static final long LOCSIG
- See Also:
- Constant Field Values
-
EXTSIG
public static final long EXTSIG
- See Also:
- Constant Field Values
-
CENSIG
public static final long CENSIG
- See Also:
- Constant Field Values
-
ENDSIG
public static final long ENDSIG
- See Also:
- Constant Field Values
-
LOCHDR
public static final int LOCHDR
- See Also:
- Constant Field Values
-
EXTHDR
public static final int EXTHDR
- See Also:
- Constant Field Values
-
CENHDR
public static final int CENHDR
- See Also:
- Constant Field Values
-
ENDHDR
public static final int ENDHDR
- See Also:
- Constant Field Values
-
LOCVER
public static final int LOCVER
- See Also:
- Constant Field Values
-
LOCFLG
public static final int LOCFLG
- See Also:
- Constant Field Values
-
LOCHOW
public static final int LOCHOW
- See Also:
- Constant Field Values
-
LOCTIM
public static final int LOCTIM
- See Also:
- Constant Field Values
-
LOCCRC
public static final int LOCCRC
- See Also:
- Constant Field Values
-
LOCSIZ
public static final int LOCSIZ
- See Also:
- Constant Field Values
-
LOCLEN
public static final int LOCLEN
- See Also:
- Constant Field Values
-
LOCNAM
public static final int LOCNAM
- See Also:
- Constant Field Values
-
LOCEXT
public static final int LOCEXT
- See Also:
- Constant Field Values
-
EXTCRC
public static final int EXTCRC
- See Also:
- Constant Field Values
-
EXTSIZ
public static final int EXTSIZ
- See Also:
- Constant Field Values
-
EXTLEN
public static final int EXTLEN
- See Also:
- Constant Field Values
-
CENVEM
public static final int CENVEM
- See Also:
- Constant Field Values
-
CENVER
public static final int CENVER
- See Also:
- Constant Field Values
-
CENFLG
public static final int CENFLG
- See Also:
- Constant Field Values
-
CENHOW
public static final int CENHOW
- See Also:
- Constant Field Values
-
CENTIM
public static final int CENTIM
- See Also:
- Constant Field Values
-
CENCRC
public static final int CENCRC
- See Also:
- Constant Field Values
-
CENSIZ
public static final int CENSIZ
- See Also:
- Constant Field Values
-
CENLEN
public static final int CENLEN
- See Also:
- Constant Field Values
-
CENNAM
public static final int CENNAM
- See Also:
- Constant Field Values
-
CENEXT
public static final int CENEXT
- See Also:
- Constant Field Values
-
CENCOM
public static final int CENCOM
- See Also:
- Constant Field Values
-
CENDSK
public static final int CENDSK
- See Also:
- Constant Field Values
-
CENATT
public static final int CENATT
- See Also:
- Constant Field Values
-
CENATX
public static final int CENATX
- See Also:
- Constant Field Values
-
CENOFF
public static final int CENOFF
- See Also:
- Constant Field Values
-
ENDSUB
public static final int ENDSUB
- See Also:
- Constant Field Values
-
ENDTOT
public static final int ENDTOT
- See Also:
- Constant Field Values
-
ENDSIZ
public static final int ENDSIZ
- See Also:
- Constant Field Values
-
ENDOFF
public static final int ENDOFF
- See Also:
- Constant Field Values
-
ENDCOM
public static final int ENDCOM
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ZipInputStream
public ZipInputStream(InputStream stream)
Constructs a newZipInputStreamto read zip entries from the given input stream.UTF-8 is used to decode all strings in the file.
-
-
Method Detail
-
close
public void close() throws IOExceptionCloses thisZipInputStream.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classInflaterInputStream- Throws:
IOException- if anIOExceptionoccurs.
-
closeEntry
public void closeEntry() throws IOExceptionCloses the current zip entry and prepares to read the next entry.- Throws:
IOException- if anIOExceptionoccurs.
-
getNextEntry
public ZipEntry getNextEntry() throws IOException
Returns the next entry from thisZipInputStreamornullif no more entries are present.- Throws:
IOException- if anIOExceptionoccurs.
-
read
public int read(byte[] buffer, int byteOffset, int byteCount) throws IOExceptionReads up tobyteCountuncompressed bytes into the buffer starting atbyteOffset. Returns the number of bytes actually read, or -1.- Overrides:
readin classInflaterInputStream- Throws:
IOException- if the stream is closed or another IOException occurs.
-
available
public int available() throws IOExceptionDescription copied from class:InflaterInputStreamReturns 0 when when this stream has exhausted its input; and 1 otherwise. A result of 1 does not guarantee that further bytes can be returned, with or without blocking.Although consistent with the RI, this behavior is inconsistent with
InputStream.available(), and violates the Liskov Substitution Principle. This method should not be used.- Overrides:
availablein classInflaterInputStream- Returns:
- 0 if no further bytes are available. Otherwise returns 1, which suggests (but does not guarantee) that additional bytes are available.
- Throws:
IOException- if this stream is closed or an error occurs
-
-