Class ZipInputStream
- All Implemented Interfaces:
Closeable,AutoCloseable
- Direct Known Subclasses:
JarInputStream
public class ZipInputStream extends InflaterInputStream
A zip file (or "archive") is a collection of (possibly) compressed files.
When reading from a ZipInputStream, you call getNextEntry()
which returns a ZipEntry of 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), call getNextEntry again. When it returns null,
there are no more entries in the input file.
Although InflaterInputStream can only read compressed zip
entries, this class can read non-compressed entries as well.
Use ZipFile if you need random access to entries by name, but use this class
if you just want to iterate over all entries.
Example
Using ZipInputStream is a little more complicated than GZIPInputStream
because zip files are containers that can contain multiple files. This code pulls all the
files out of a zip file, similar to the unzip(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 intLOCVERFields inherited from class java.util.zip.InflaterInputStream
buf, inf, lenFields 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
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, skipMethods inherited from class java.io.InputStream
read
-
Field Details
-
LOCSIG
static final long LOCSIG- See Also:
- Constant Field Values
-
EXTSIG
static final long EXTSIG- See Also:
- Constant Field Values
-
CENSIG
static final long CENSIG- See Also:
- Constant Field Values
-
ENDSIG
static final long ENDSIG- See Also:
- Constant Field Values
-
LOCHDR
static final int LOCHDR- See Also:
- Constant Field Values
-
EXTHDR
static final int EXTHDR- See Also:
- Constant Field Values
-
CENHDR
static final int CENHDR- See Also:
- Constant Field Values
-
ENDHDR
static final int ENDHDR- See Also:
- Constant Field Values
-
LOCVER
static final int LOCVER- See Also:
- Constant Field Values
-
LOCFLG
static final int LOCFLG- See Also:
- Constant Field Values
-
LOCHOW
static final int LOCHOW- See Also:
- Constant Field Values
-
LOCTIM
static final int LOCTIM- See Also:
- Constant Field Values
-
LOCCRC
static final int LOCCRC- See Also:
- Constant Field Values
-
LOCSIZ
static final int LOCSIZ- See Also:
- Constant Field Values
-
LOCLEN
static final int LOCLEN- See Also:
- Constant Field Values
-
LOCNAM
static final int LOCNAM- See Also:
- Constant Field Values
-
LOCEXT
static final int LOCEXT- See Also:
- Constant Field Values
-
EXTCRC
static final int EXTCRC- See Also:
- Constant Field Values
-
EXTSIZ
static final int EXTSIZ- See Also:
- Constant Field Values
-
EXTLEN
static final int EXTLEN- See Also:
- Constant Field Values
-
CENVEM
static final int CENVEM- See Also:
- Constant Field Values
-
CENVER
static final int CENVER- See Also:
- Constant Field Values
-
CENFLG
static final int CENFLG- See Also:
- Constant Field Values
-
CENHOW
static final int CENHOW- See Also:
- Constant Field Values
-
CENTIM
static final int CENTIM- See Also:
- Constant Field Values
-
CENCRC
static final int CENCRC- See Also:
- Constant Field Values
-
CENSIZ
static final int CENSIZ- See Also:
- Constant Field Values
-
CENLEN
static final int CENLEN- See Also:
- Constant Field Values
-
CENNAM
static final int CENNAM- See Also:
- Constant Field Values
-
CENEXT
static final int CENEXT- See Also:
- Constant Field Values
-
CENCOM
static final int CENCOM- See Also:
- Constant Field Values
-
CENDSK
static final int CENDSK- See Also:
- Constant Field Values
-
CENATT
static final int CENATT- See Also:
- Constant Field Values
-
CENATX
static final int CENATX- See Also:
- Constant Field Values
-
CENOFF
static final int CENOFF- See Also:
- Constant Field Values
-
ENDSUB
static final int ENDSUB- See Also:
- Constant Field Values
-
ENDTOT
static final int ENDTOT- See Also:
- Constant Field Values
-
ENDSIZ
static final int ENDSIZ- See Also:
- Constant Field Values
-
ENDOFF
static final int ENDOFF- See Also:
- Constant Field Values
-
ENDCOM
static final int ENDCOM- See Also:
- Constant Field Values
-
-
Constructor Details
-
ZipInputStream
Constructs a newZipInputStreamto read zip entries from the given input stream.
-
-
Method Details
-
close
Closes thisZipInputStream.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classInflaterInputStream- Throws:
IOException- if anIOExceptionoccurs.
-
closeEntry
Closes the current zip entry and prepares to read the next entry.- Throws:
IOException- if anIOExceptionoccurs.
-
getNextEntry
Returns the next entry from thisZipInputStreamornullif no more entries are present.- Throws:
IOException- if anIOExceptionoccurs.
-
read
Reads 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
Description 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
-
createZipEntry
creates aZipEntrywith the given name.- Parameters:
name- the name of the entry.- Returns:
- the created
ZipEntry.
-