Class ZipInputStream in Java

digitization, transformation, binary

Class ZipInputStream in Java

ZipInputStream class is a subclass of InflaterInputStream that reads the entries of a ZIP file in sequential order. A ZipInputStream is created by specifying the InputStream from which it is to read the contents of the ZIP file. Once the ZipInputStream is created, the getNextEntry() method is used to begin the reading of data from the next entry in the ZIP file. This method must be called before read() is called to begin reading of the first entry.

The ZipInputStream class reads files that have been compressed using the ZIP format. To read uncompressed data from a ZIP file, simply construct a ZipInputStream that wraps a regular input stream. The getNextEntry() method returns each entry in the ZIP file in order. Once we have a ZipEntry object, you use the read() method to retrieve uncompressed data from it. If you want to read the entries in a nonsequential order, use a ZipFile instead.

Ads code goes here

Note

  • getNextEntry() returns a ZipEntry object that describes the entry being read.
  • getNextEntry() returns null when there are no more entries to be read from the ZIP file.

The read() methods of ZipInputStream read until the end of the current entry and then return -1 indicating that there are no more data to read. To continue with the next entry in the ZIP file, we must call getNextEntry() again.

Similarly, the skip() method only skips bytes within the current entry. closeEntry() can be called to skip the remaining data in the current entry, but it is usually easier simply to call getNextEntry() to begin the next entry.

READ  Class InternalError in Java

The structure of the class ZipInputStream is given by

public class java.util.zip.ZipInputStream extends java.util.zip.InflaterInputStream{
// Public Constructor
public ZipInputStream(InputStream in);
// Public Instance Methods
public void close() throws IOException; // Overrides FilterInputStream
public void closeEntry() throws IOException;
public ZipEntry getNextEntry() throws IOException;
public int read(byte[] b, int off, int len) throws IOException; //Overrides InflaterInputStream
public long skip(long n) throws IOException; // Overrides InflaterInputStream
}

The details of the class structure are given as follows:

public ZipInputStream(InputStream in);

public ZipInputStream(InputStream in) constructor creates a ZipInputStream that inflates data from the given InputStream.

Parameter
in – The underlying input stream.

public void close();

public void close() method closes this stream and releases any system resources that are associated with it.

public void closeEntry();

public void closeEntry() method closes the currently open entry in the ZIP file. The stream is then positioned to read the next entry using getNextEntry().

public ZipEntry getNextEntry();

public ZipEntry getNextEntry() method returns a ZipEntry that represents the next entry in the ZIP file and positions the stream to read that entry.

This method returns the ZipEntry for the next entry or null if there are no more entries.

public int read(byte[] b, int off, int len);

public int read(byte[] b, int off, int len) method reads enough data from the underlying InputStream to return len bytes of uncompressed data. The uncompressed data is placed into the given array starting at off. The method blocks until some data are available for decompression.

This method returns the number of bytes read or -1 if the end of the entry is encountered immediately.

READ  Class StackOverFlowError in Java

Parameter
b – An array of bytes to be filled from the stream.
off – An offset into the byte array.
len – The number of bytes to read.

public long skip(long n);

public long skip(long n) method skips over the specified number of uncompressed data bytes by reading data from the
underlying InputStream and decompressing it.

This method returns the actual number of bytes skipped.

Apart from these ZipInputStreamclass also has inherited methods from class- Object. They are as follows:

  • clone()
  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • toString()
  • wait(long)

ZipInputStreamclass also has inherited methods from FilterInputStream. They are as follows:

  • available()
  • mark(int)
  • reset()
  • markSupported()
  • read(byte[])

ZipInputStreamclass also has one inherited method from InflaterInputStream. That is as follows:

  • read()

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *