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.
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();
}
| Modifier and Type | Field and Description |
|---|---|
static int |
CENATT |
static int |
CENATX |
static int |
CENCOM |
static int |
CENCRC |
static int |
CENDSK |
static int |
CENEXT |
static int |
CENFLG |
static int |
CENHDR |
static int |
CENHOW |
static int |
CENLEN |
static int |
CENNAM |
static int |
CENOFF |
static long |
CENSIG |
static int |
CENSIZ |
static int |
CENTIM |
static int |
CENVEM |
static int |
CENVER |
static int |
ENDCOM |
static int |
ENDHDR |
static int |
ENDOFF |
static long |
ENDSIG |
static int |
ENDSIZ |
static int |
ENDSUB |
static int |
ENDTOT |
static int |
EXTCRC |
static int |
EXTHDR |
static int |
EXTLEN |
static long |
EXTSIG |
static int |
EXTSIZ |
static int |
LOCCRC |
static int |
LOCEXT |
static int |
LOCFLG |
static int |
LOCHDR |
static int |
LOCHOW |
static int |
LOCLEN |
static int |
LOCNAM |
static long |
LOCSIG |
static int |
LOCSIZ |
static int |
LOCTIM |
static int |
LOCVER |
buf, inf, lenin| Constructor and Description |
|---|
ZipInputStream(InputStream stream)
Constructs a new
ZipInputStream to read zip entries from the given input stream. |
| Modifier and Type | Method and Description |
|---|---|
int |
available()
Returns 0 when when this stream has exhausted its input; and 1 otherwise.
|
void |
close()
Closes this
ZipInputStream. |
void |
closeEntry()
Closes the current zip entry and prepares to read the next entry.
|
protected ZipEntry |
createZipEntry(String name)
creates a
ZipEntry with the given name. |
ZipEntry |
getNextEntry()
Returns the next entry from this
ZipInputStream or null if
no more entries are present. |
int |
read(byte[] buffer,
int byteOffset,
int byteCount)
Reads up to
byteCount uncompressed bytes into the buffer
starting at byteOffset. |
fill, mark, markSupported, read, reset, skipnullInputStream, read, readAllBytes, readNBytes, readNBytes, transferTopublic static final long LOCSIG
public static final long EXTSIG
public static final long CENSIG
public static final long ENDSIG
public static final int LOCHDR
public static final int EXTHDR
public static final int CENHDR
public static final int ENDHDR
public static final int LOCVER
public static final int LOCFLG
public static final int LOCHOW
public static final int LOCTIM
public static final int LOCCRC
public static final int LOCSIZ
public static final int LOCLEN
public static final int LOCNAM
public static final int LOCEXT
public static final int EXTCRC
public static final int EXTSIZ
public static final int EXTLEN
public static final int CENVEM
public static final int CENVER
public static final int CENFLG
public static final int CENHOW
public static final int CENTIM
public static final int CENCRC
public static final int CENSIZ
public static final int CENLEN
public static final int CENNAM
public static final int CENEXT
public static final int CENCOM
public static final int CENDSK
public static final int CENATT
public static final int CENATX
public static final int CENOFF
public static final int ENDSUB
public static final int ENDTOT
public static final int ENDSIZ
public static final int ENDOFF
public static final int ENDCOM
public ZipInputStream(InputStream stream)
ZipInputStream to read zip entries from the given input stream.
UTF-8 is used to decode all strings in the file.
public void close()
throws IOException
ZipInputStream.close in interface Closeableclose in interface AutoCloseableclose in class InflaterInputStreamIOException - if an IOException occurs.public void closeEntry()
throws IOException
IOException - if an IOException occurs.public ZipEntry getNextEntry() throws IOException
ZipInputStream or null if
no more entries are present.IOException - if an IOException occurs.public int read(byte[] buffer,
int byteOffset,
int byteCount)
throws IOException
byteCount uncompressed bytes into the buffer
starting at byteOffset. Returns the number of bytes actually read, or -1.read in class InflaterInputStreamIOException - if the stream is closed or another IOException occurs.public int available()
throws IOException
InflaterInputStreamAlthough consistent with the RI, this behavior is inconsistent with
InputStream.available(), and violates the Liskov
Substitution Principle. This method should not be used.
available in class InflaterInputStreamIOException - if this stream is closed or an error occurs