Android APIs
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

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 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();
 }
 

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 to read zip entries from the given 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 prepares to read the next entry.
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.
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
From interface java.lang.AutoCloseable

Constants

public static final int CENATT

Added in API level 21

Constant Value: 36 (0x00000024)

public static final int CENATX

Added in API level 21

Constant Value: 38 (0x00000026)

public static final int CENCOM

Added in API level 21

Constant Value: 32 (0x00000020)

public static final int CENCRC

Added in API level 21

Constant Value: 16 (0x00000010)

public static final int CENDSK

Added in API level 21

Constant Value: 34 (0x00000022)

public static final int CENEXT

Added in API level 21

Constant Value: 30 (0x0000001e)

public static final int CENFLG

Added in API level 21

Constant Value: 8 (0x00000008)

public static final int CENHDR

Added in API level 21

Constant Value: 46 (0x0000002e)

public static final int CENHOW

Added in API level 21

Constant Value: 10 (0x0000000a)

public static final int CENLEN

Added in API level 21

Constant Value: 24 (0x00000018)

public static final int CENNAM

Added in API level 21

Constant Value: 28 (0x0000001c)

public static final int CENOFF

Added in API level 21

Constant Value: 42 (0x0000002a)

public static final long CENSIG

Added in API level 21

Constant Value: 33639248 (0x0000000002014b50)

public static final int CENSIZ

Added in API level 21

Constant Value: 20 (0x00000014)

public static final int CENTIM

Added in API level 21

Constant Value: 12 (0x0000000c)

public static final int CENVEM

Added in API level 21

Constant Value: 4 (0x00000004)

public static final int CENVER

Added in API level 21

Constant Value: 6 (0x00000006)

public static final int ENDCOM

Added in API level 21

Constant Value: 20 (0x00000014)

public static final int ENDHDR

Added in API level 21

Constant Value: 22 (0x00000016)

public static final int ENDOFF

Added in API level 21

Constant Value: 16 (0x00000010)

public static final long ENDSIG

Added in API level 21

Constant Value: 101010256 (0x0000000006054b50)

public static final int ENDSIZ

Added in API level 21

Constant Value: 12 (0x0000000c)

public static final int ENDSUB

Added in API level 21

Constant Value: 8 (0x00000008)

public static final int ENDTOT

Added in API level 21

Constant Value: 10 (0x0000000a)

public static final int EXTCRC

Added in API level 21

Constant Value: 4 (0x00000004)

public static final int EXTHDR

Added in API level 21

Constant Value: 16 (0x00000010)

public static final int EXTLEN

Added in API level 21

Constant Value: 12 (0x0000000c)

public static final long EXTSIG

Added in API level 21

Constant Value: 134695760 (0x0000000008074b50)

public static final int EXTSIZ

Added in API level 21

Constant Value: 8 (0x00000008)

public static final int LOCCRC

Added in API level 21

Constant Value: 14 (0x0000000e)

public static final int LOCEXT

Added in API level 21

Constant Value: 28 (0x0000001c)

public static final int LOCFLG

Added in API level 21

Constant Value: 6 (0x00000006)

public static final int LOCHDR

Added in API level 21

Constant Value: 30 (0x0000001e)

public static final int LOCHOW

Added in API level 21

Constant Value: 8 (0x00000008)

public static final int LOCLEN

Added in API level 21

Constant Value: 22 (0x00000016)

public static final int LOCNAM

Added in API level 21

Constant Value: 26 (0x0000001a)

public static final long LOCSIG

Added in API level 21

Constant Value: 67324752 (0x0000000004034b50)

public static final int LOCSIZ

Added in API level 21

Constant Value: 18 (0x00000012)

public static final int LOCTIM

Added in API level 21

Constant Value: 10 (0x0000000a)

public static final int LOCVER

Added in API level 21

Constant Value: 4 (0x00000004)

Public Constructors

public ZipInputStream (InputStream stream)

Added in API level 1

Constructs a new ZipInputStream to read zip entries from the given input stream.

UTF-8 is used to decode all strings in the file.

Public Methods

public int available ()

Added in 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 ()

Added in API level 1

Closes this ZipInputStream.

Throws
IOException if an IOException occurs.

public void closeEntry ()

Added in API level 1

Closes the current zip entry and prepares to read the next entry.

Throws
IOException if an IOException occurs.

public ZipEntry getNextEntry ()

Added in API level 1

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

Throws
IOException if an IOException occurs.

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

Added in API level 1

Reads up to byteCount uncompressed bytes into the buffer starting at byteOffset. Returns the number of bytes actually read, or -1.

Throws
IOException

Protected Methods

protected ZipEntry createZipEntry (String name)

Added in API level 1

creates a ZipEntry with the given name.

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