public class

ZipInputStream

extends InflaterInputStream
java.lang.Object
   ↳ java.io.InputStream
     ↳ java.io.FilterInputStream
       ↳ java.util.zip.InflaterInputStream
         ↳ java.util.zip.ZipInputStream
Known Direct Subclasses

Class Overview

This class provides an implementation of FilterInputStream that decompresses data from an InputStream containing a ZIP archive.

A ZIP archive is a collection of (possibly) compressed files. When reading from a ZipInputStream, you retrieve the entry's metadata with getNextEntry before you can read the userdata.

Although InflaterInputStream can only read compressed ZIP archive entries, this class can read non-compressed entries as well.

Use ZipFile if you can access the archive as a file directly, especially if you want random access to entries, rather than needing to iterate over all entries.

Example

Using ZipInputStream is a little more complicated than GZIPInputStream because ZIP archives are containers that can contain multiple files. This code pulls all the files out of a ZIP archive, 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();
 }
 

See Also

Summary

Constants
int CENATT
int CENATX
int CENCOM
int CENCRC
int CENDSK
int CENEXT
int CENFLG
int CENHDR
int CENHOW
int CENLEN
int CENNAM
int CENOFF
long CENSIG
int CENSIZ
int CENTIM
int CENVEM
int CENVER
int ENDCOM
int ENDHDR
int ENDOFF
long ENDSIG
int ENDSIZ
int ENDSUB
int ENDTOT
int EXTCRC
int EXTHDR
int EXTLEN
long EXTSIG
int EXTSIZ
int LOCCRC
int LOCEXT
int LOCFLG
int LOCHDR
int LOCHOW
int LOCLEN
int LOCNAM
long LOCSIG
int LOCSIZ
int LOCTIM
int LOCVER
[Expand]
Inherited Fields
From class java.util.zip.InflaterInputStream
From class java.io.FilterInputStream
Public Constructors
ZipInputStream(InputStream stream)
Constructs a new ZipInputStream from the specified input stream.
Public Methods
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 positions to read the next entry.
ZipEntry getNextEntry()
Reads the next entry from this ZipInputStream or null if no more entries are present.
int read(byte[] buffer, int offset, int byteCount)
Reads up to the specified number of uncompressed bytes into the buffer starting at the offset.
Protected Methods
ZipEntry createZipEntry(String name)
creates a ZipEntry with the given name.
[Expand]
Inherited Methods
From class java.util.zip.InflaterInputStream
From class java.io.FilterInputStream
From class java.io.InputStream
From class java.lang.Object
From interface java.io.Closeable

Constants

public static final int CENATT

Since: API Level

Constant Value: 36 (0x00000024)

public static final int CENATX

Since: API Level

Constant Value: 38 (0x00000026)

public static final int CENCOM

Since: API Level

Constant Value: 32 (0x00000020)

public static final int CENCRC

Since: API Level

Constant Value: 16 (0x00000010)

public static final int CENDSK

Since: API Level

Constant Value: 34 (0x00000022)

public static final int CENEXT

Since: API Level

Constant Value: 30 (0x0000001e)

public static final int CENFLG

Since: API Level

Constant Value: 8 (0x00000008)

public static final int CENHDR

Since: API Level

Constant Value: 46 (0x0000002e)

public static final int CENHOW

Since: API Level

Constant Value: 10 (0x0000000a)

public static final int CENLEN

Since: API Level

Constant Value: 24 (0x00000018)

public static final int CENNAM

Since: API Level

Constant Value: 28 (0x0000001c)

public static final int CENOFF

Since: API Level

Constant Value: 42 (0x0000002a)

public static final long CENSIG

Since: API Level

Constant Value: 33639248 (0x0000000002014b50)

public static final int CENSIZ

Since: API Level

Constant Value: 20 (0x00000014)

public static final int CENTIM

Since: API Level

Constant Value: 12 (0x0000000c)

public static final int CENVEM

Since: API Level

Constant Value: 4 (0x00000004)

public static final int CENVER

Since: API Level

Constant Value: 6 (0x00000006)

public static final int ENDCOM

Since: API Level

Constant Value: 20 (0x00000014)

public static final int ENDHDR

Since: API Level

Constant Value: 22 (0x00000016)

public static final int ENDOFF

Since: API Level

Constant Value: 16 (0x00000010)

public static final long ENDSIG

Since: API Level

Constant Value: 101010256 (0x0000000006054b50)

public static final int ENDSIZ

Since: API Level

Constant Value: 12 (0x0000000c)

public static final int ENDSUB

Since: API Level

Constant Value: 8 (0x00000008)

public static final int ENDTOT

Since: API Level

Constant Value: 10 (0x0000000a)

public static final int EXTCRC

Since: API Level

Constant Value: 4 (0x00000004)

public static final int EXTHDR

Since: API Level

Constant Value: 16 (0x00000010)

public static final int EXTLEN

Since: API Level

Constant Value: 12 (0x0000000c)

public static final long EXTSIG

Since: API Level

Constant Value: 134695760 (0x0000000008074b50)

public static final int EXTSIZ

Since: API Level

Constant Value: 8 (0x00000008)

public static final int LOCCRC

Since: API Level

Constant Value: 14 (0x0000000e)

public static final int LOCEXT

Since: API Level

Constant Value: 28 (0x0000001c)

public static final int LOCFLG

Since: API Level

Constant Value: 6 (0x00000006)

public static final int LOCHDR

Since: API Level

Constant Value: 30 (0x0000001e)

public static final int LOCHOW

Since: API Level

Constant Value: 8 (0x00000008)

public static final int LOCLEN

Since: API Level

Constant Value: 22 (0x00000016)

public static final int LOCNAM

Since: API Level

Constant Value: 26 (0x0000001a)

public static final long LOCSIG

Since: API Level

Constant Value: 67324752 (0x0000000004034b50)

public static final int LOCSIZ

Since: API Level

Constant Value: 18 (0x00000012)

public static final int LOCTIM

Since: API Level

Constant Value: 10 (0x0000000a)

public static final int LOCVER

Since: API Level

Constant Value: 4 (0x00000004)

Public Constructors

public ZipInputStream (InputStream stream)

Since: API Level 1

Constructs a new ZipInputStream from the specified input stream.

Parameters
stream the input stream to representing a ZIP archive.

Public Methods

public int available ()

Since: API Level 1

Returns 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 available(), and violates the Liskov Substitution Principle. This method should not be used.

Returns
  • 0 if no further bytes are available. Otherwise returns 1, which suggests (but does not guarantee) that additional bytes are available.
Throws
IOException

public void close ()

Since: API Level 1

Closes this ZipInputStream.

Throws
IOException if an IOException occurs.

public void closeEntry ()

Since: API Level 1

Closes the current ZIP entry and positions to read the next entry.

Throws
IOException if an IOException occurs.

public ZipEntry getNextEntry ()

Since: API Level 1

Reads the next entry from this ZipInputStream or null if no more entries are present.

Returns
  • the next ZipEntry contained in the input stream.
Throws
IOException if an IOException occurs.
See Also

public int read (byte[] buffer, int offset, int byteCount)

Since: API Level 1

Reads up to the specified number of uncompressed bytes into the buffer starting at the offset.

Parameters
buffer the byte array in which to store the bytes read.
offset the initial position in buffer to store the bytes read from this stream.
byteCount the maximum number of bytes to store in buffer.
Returns
  • the number of bytes read
Throws
IOException

Protected Methods

protected ZipEntry createZipEntry (String name)

Since: API Level 1

creates a ZipEntry with the given name.

Parameters
name the name of the entry.
Returns
  • the created ZipEntry.