Category: 18. Java Zip

https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR3KTh3wGipdhva2iT4Urby8_N1SNDVlrunA2h9k_CFXfUDnU5Uybjs09bAcC4a39Hrg7w

  • Errors

    Introduction

    The java.util.zip Error contains the error which can occur during zip/unzip operations.

    Interface Summary

    Sr.No.Error & Description
    1ZipErrorSignals that an unrecoverable error has occurred.
  • Exceptions

    Introduction

    The java.util.zip Exceptions contains the exceptions which can occur during zip/unzip operations.

    Interface Summary

    Sr.No.Exception & Description
    1DataFormatExceptionSignals that a data format error has occurred.
    2ZipExceptionSignals that a Zip exception of some sort has occurred.
  • ZipOutputStream 

    Introduction

    The java.util.zip.ZipOutputStream class implements an output stream filter for writing files in the ZIP file format. Includes support for both compressed and uncompressed entries.

    Class Declaration

    Following is the declaration for java.util.zip.ZipOutputStream class −

    publicclassZipOutputStreamextendsDeflaterOutputStream

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Fields

    Following are the fields for java.util.zip.ZipOutputStream class −

    • 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 DEFLATED − Compression method for compressed (DEFLATED) entries.
    • 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
    • static int STORED − Compression method for uncompressed (STORED) entries.

    Constructors

    Sr.No.Constructor & Description
    1ZipOutputStream(OutputStream out)Creates a new ZIP output stream.
    2ZipOutputStream(OutputStream out, Charset charset)Creates a new ZIP output stream.

    Class Methods

    Sr.No.Method & Description
    1void close()Closes the ZIP output stream as well as the stream being filtered.
    2void closeEntry()Closes the current ZIP entry and positions the stream for writing the next entry.
    3void finish()Finishes writing the contents of the ZIP output stream without closing the underlying stream.
    4void putNextEntry(ZipEntry e)Begins writing a new ZIP file entry and positions the stream to the start of the entry data.
    5void setComment(String comment)Sets the ZIP file comment.
    6void setLevel(int level)Sets the compression level for subsequent entries which are DEFLATED.
    7void setMethod(int method)Sets the default compression method for subsequent entries.
    8void write(byte[] b, int off, int len)Writes an array of bytes to the current ZIP entry data.

    Methods Inherited

    This class inherits methods from the following classes −

    • java.util.zip.DeflaterOutputStream
    • java.io.FilterOutputStream
    • java.lang.Object
  • ZipInputStream 

    Introduction

    The java.util.zip.ZipInputStream class implements an input stream filter for reading files in the ZIP file format. Includes support for both compressed and uncompressed entries.

    Class Declaration

    Following is the declaration for java.util.zip.ZipInputStream class −

    publicclassZipInputStreamextendsInflaterInputStream

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Fields

    Following are the fields for java.util.zip.ZipInputStream class −

    • 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

    Constructors

    Sr.No.Constructor & Description
    1ZipInputStream(InputStream in)Creates a new ZIP input stream.
    2ZipInputStream(InputStream in, Charset charset)Creates a new ZIP input stream.

    Class Methods

    Sr.No.Method & Description
    1int available()Returns 0 after EOF has reached for the current entry data, otherwise always return 1.
    2void close()Closes this input stream and releases any system resources associated with the stream.
    3void closeEntry()Closes the current ZIP entry and positions the stream for reading the next entry.
    4ZipEntry getNextEntry()Reads the next ZIP file entry and positions the stream at the beginning of the entry data.
    5int read(byte[] b, int off, int len)Reads from the current ZIP entry into an array of bytes.
    6long skip(long n)Skips specified number of bytes in the current ZIP entry.

    Methods Inherited

    This class inherits methods from the following classes −

    • java.util.zip.InflaterInputStream
    • java.io.FilterInputStream
    • java.lang.Object
  • ZipFile 

    Introduction

    The java.util.zip.ZipFile class is used to read entries from a zip file.

    Class Declaration

    Following is the declaration for java.util.zip.ZipFile class −

    publicclassZipFileextendsObjectimplementsCloseable

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Fields

    Following are the fields for java.util.zip.ZipFile class −

    • 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
    • static int OPEN_DELETE − Mode flag to open a zip file and mark it for deletion.
    • static int OPEN_READ − Mode flag to open a zip file for reading.

    Constructors

    Sr.No.Constructor & Description
    1ZipFile(File file)Opens a ZIP file for reading given the specified File object.
    2ZipFile(File file, Charset charset)Opens a ZIP file for reading given the specified File object.
    3ZipFile(File file, int mode)Opens a new ZipFile to read from the specified File object in the specified mode.
    4ZipFile(File file, int mode, Charset charset)Opens a new ZipFile to read from the specified File object in the specified mode.
    5ZipFile(String name)Opens a zip file for reading.
    6ZipFile(String name, Charset charset)Opens a zip file for reading.

    Class Methods

    Sr.No.Method & Description
    1void close()Closes the ZIP file.
    2Enumeration<? extends ZipEntry> entries()Returns an enumeration of the ZIP file entries.
    3String getComment()Returns the zip file comment, or null if none.
    4ZipEntry getEntry(String name)Returns the zip file entry for the specified name, or null if not found.
    5InputStream getInputStream(ZipEntry entry)Returns an input stream for reading the contents of the specified zip file entry.
    6String getName()Returns the path name of the ZIP file.
    7int size()Returns the number of entries in the ZIP file.

    Methods Inherited

    This class inherits methods from the following classes −

    • Java.lang.Object
  • ZipEntry 

    Introduction

    The java.util.zip.ZipEntry class is used to represent a ZIP file entry.

    Class Declaration

    Following is the declaration for java.util.zip.ZipEntry class −

    publicclassZipEntryextendsObjectimplementsCloneable

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Fields

    Following are the fields for java.util.zip.ZipEntry class −

    • protected byte[] buf − Output buffer for writing uncompressed data.
    • protected Inflater inf − Decompressor for this stream.
    • 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 DEFLATED − Compression method for compressed (deflated) entries.
    • 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
    • static int STORED − Compression method for uncompressed entries.

    Constructors

    Sr.No.Constructor & Description
    1ZipEntry(String name)Creates a new zip entry with the specified name.
    2ZipEntry(ZipEntry e)Creates a new zip entry with fields taken from the specified zip entry.

    Class Methods

    Sr.No.Method & Description
    1Object clone()Returns a copy of this entry.
    2String getComment()Returns the comment string for the entry, or null if none.
    3long getCompressedSize()Returns the size of the compressed entry data, or -1 if not known.
    4long getCrc()Returns the CRC-32 checksum of the uncompressed entry data, or -1 if not known.
    5byte[] getExtra()Returns the extra field data for the entry, or null if none.
    6int getMethod()Returns the compression method of the entry, or -1 if not specified.
    7String getName()Returns the name of the entry.
    8long getSize()Returns the uncompressed size of the entry data, or -1 if not known.
    9long getTime()Returns the modification time of the entry, or -1 if not specified.
    10int hashCode()Returns the hash code value for this entry.
    11boolean isDirectory()Returns true if this is a directory entry.
    12void setComment(String comment)Sets the optional comment string for the entry.
    13void setCrc(long crc)Sets the CRC-32 checksum of the uncompressed entry data.
    14void setExtra(byte[] extra)Sets the optional extra field data for the entry.
    15void setMethod(int method)Sets the compression method for the entry.
    16void setSize(long size)Sets the uncompressed size of the entry data.
    17void setTime(long time)Sets the modification time of the entry.
    18String toString()Returns a string representation of the ZIP entry.

    Methods Inherited

    This class inherits methods from the following classes −

    • java.io.FilterOutputStream
    • java.lang.Object
  • InflaterOutputStream 

    Introduction

    The java.util.zip.InflaterOutputStream class implements an output stream filter for uncompressing data stored in the “deflate” compression format.

    Class Declaration

    Following is the declaration for java.util.zip.InflaterOutputStream class −

    publicclassInflaterOutputStreamextendsFilterOutputStream

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Fields

    Following are the fields for java.util.zip.InflaterOutputStream class −

    • protected byte[] buf − Output buffer for writing uncompressed data.
    • protected Inflater inf − Decompressor for this stream.

    Constructors

    Sr.No.Constructor & Description
    1InflaterOutputStream(OutputStream out)Creates a new output stream with a default decompressor and buffer size.
    2InflaterOutputStream(OutputStream out, Inflater infl)Creates a new output stream with the specified decompressor and a default buffer size.
    3InflaterOutputStream(OutputStream out, Inflater infl, int bufLen)Creates a new output stream with the specified decompressor and buffer size.

    Class Methods

    Sr.No.Method & Description
    1void close()Writes remaining compressed data to the output stream and closes the underlying stream.
    2void finish()Finishes writing uncompressed data to the output stream without closing the underlying stream.
    3void flush()Flushes this output stream, forcing any pending buffered output bytes to be written.
    4void write(byte[] b, int off, int len)Writes an array of bytes to the compressed output stream.
    5void write(int b)Writes a byte to the compressed output stream.

    Methods Inherited

    This class inherits methods from the following classes −

    • java.io.FilterOutputStream
    • java.lang.Object
  • InflaterInputStream 

    Introduction

    The java.util.zip.InflaterInputStream class implements a stream filter for uncompressing data in the “deflate” compression format. It is also used as the basis for other decompression filters, such as GZIPInputStream.

    Class Declaration

    Following is the declaration for java.util.zip.InflaterInputStream class −

    publicclassInflaterInputStreamextendsFilterInputStream

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Fields

    Following are the fields for java.util.zip.InflaterInputStream class −

    • protected byte[] buf − Input buffer for decompression.
    • protected Inflater inf − Decompressor for this stream.
    • protected int len − Length of input buffer.

    Constructors

    Sr.No.Constructor & Description
    1InflaterInputStream(InputStream in)Creates a new input stream with a default decompressor and buffer size.
    2InflaterInputStream(InputStream in, Inflater inf)Creates a new input stream with the specified decompressor and a default buffer size.
    3InflaterInputStream(InputStream in, Inflater inf, int size)Creates a new input stream with the specified decompressor and buffer size.

    Class Methods

    Sr.No.Method & Description
    1int available()Returns 0 after EOF has been reached, otherwise always return 1.
    2void close()Closes this input stream and releases any system resources associated with the stream.
    3void mark(int readlimit)Marks the current position in this input stream.
    4boolean markSupported()Tests if this input stream supports the mark and reset methods.
    5int read()Reads a byte of uncompressed data.
    6int read(byte[] b, int off, int len)Reads uncompressed data into an array of bytes.
    7void reset()Repositions this stream to the position at the time the mark method was last called on this input stream.
    8long skip(long n)Skips specified number of bytes of uncompressed data.

    Methods Inherited

    This class inherits methods from the following classes −

    • java.io.FilterInputStream
    • java.lang.Object
  • Inflater 

    Introduction

    The java.util.zip.Inflater class provides support for general purpose decompression using the popular ZLIB compression library. The ZLIB compression library was initially developed as part of the PNG graphics standard and is not protected by patents. It is fully described in the specifications at the java.util.zip package description.

    Class Declaration

    Following is the declaration for java.util.zip.Inflater class −

    publicclassInflaterextendsObject

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Constructors

    Sr.No.Constructor & Description
    1Inflater()Creates a new decompressor.
    2Inflater(boolean nowrap)Creates a new decompressor.

    Class Methods

    Sr.No.Method & Description
    1void end()Closes the decompressor and discards any unprocessed input.
    2boolean finished()Returns true if the end of the compressed data stream has been reached.
    3int getAdler()Returns the ADLER-32 value of the uncompressed data.
    4long getBytesRead()Returns the total number of compressed bytes input so far.
    5long getBytesWritten()Returns the total number of uncompressed bytes output so far.
    6int getRemaining()Returns the total number of bytes remaining in the input buffer.
    7int getTotalIn()Returns the total number of compressed bytes input so far.
    8int getTotalOut()Returns the total number of uncompressed bytes output so far.
    9int inflate(byte[] b)Uncompresses bytes into specified buffer.
    10int inflate(byte[] b, int off, int len)Uncompresses bytes into specified buffer.
    11boolean needsDictionary()Returns true if a preset dictionary is needed for decompression.
    12boolean needsInput()Returns true if no data remains in the input buffer.
    13void reset()Resets inflater so that a new set of input data can be processed.
    14void setDictionary(byte[] b)Sets the preset dictionary to the given array of bytes.
    15void setDictionary(byte[] b, int off, int len)Sets the preset dictionary to the given array of bytes.
    16void setInput(byte[] b)Sets input data for decompression.
    17void setInput(byte[] b, int off, int len)Sets input data for decompression.

    Methods Inherited

    This class inherits methods from the following classes −

    • java.lang.Object
  • GZIPOutputStream 

    Introduction

    The java.util.zip.GZIPOutputStream class implements a stream filter for writing compressed data in the GZIP file format.

    Class Declaration

    Following is the declaration for java.util.zip.GZIPOutputStream class −

    publicclassGZIPOutputStreamextendsDeflaterOutputStream

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Fields

    Following are the fields for java.util.zip.GZIPOutputStream class −

    • Protected CRC32 crc − CRC-32 for uncompressed data.

    Constructors

    Sr.No.Constructor & Description
    1GZIPOutputStream(OutputStream out)Creates a new output stream with a default buffer size.
    2GZIPOutputStream(OutputStream out, boolean syncFlush)Creates a new output stream with a default buffer size and the specified flush mode.
    3GZIPOutputStream(OutputStream out, int size)Creates a new output stream with the specified buffer size.
    4GZIPOutputStream(OutputStream out, int size, boolean syncFlush)Creates a new output stream with the specified buffer size and flush mode.

    Class Methods

    Sr.No.Method & Description
    1void finish()Finishes writing compressed data to the output stream without closing the underlying stream.
    2int write(byte[] buf, int off, int len)Writes array of bytes to the compressed output stream.

    Methods Inherited

    This class inherits methods from the following classes −

    • java.util.zip.DeflaterOutputStream
    • java.io.FilterOutputStream
    • java.lang.Object