Class ZipOutputStream in Java

digitization, transformation, binary

Class ZipOutputStream in Java

ZipOutputStream class is a subclass of DeflaterOutputStream that writes data in the ZIP file format to an output stream.

The ZipOutputStream class writes compressed files in the ZIP format. To write a ZIP file, construct a ZipOutputStream that wraps a regular output stream. You have to create a ZipEntry for each entry in the file.

Ads code goes here

The putNextEntry() method puts the entry in the file so that you can then use the write() method to write data for that entry. When you finish writing the data for entry, call closeEntry() to close that entry and putNextEntry() to start another entry.

The setMethod() method specifies whether the data is compressed or uncompressed by default; setLevel() specifies the level of compression that is used by default. These values can be overridden by the method and level set for a particular entry. If we are storing compressed (deflated) files, we need only specify a name for each ZipEntry; the other fields are filled in automatically. If, however, we are placing uncompressed entries, we need to specify the size of each entry and provide a CRC-32 checksum value.

Before writing any data to the ZipOutputStream, you must begin an entry within the ZIP file with putNextEntry(). The ZipEntry object passed to this method should specify at least a name for the entry.

Once we have begun an entry with putNextEntry(), we can write the contents of that entry with the write() methods. When you reach the end of an entry, you can begin a new one by calling putNextEntry() again, or we can close the current entry with closeEntry(), or we can close the stream itself with close().

READ  Class ObjectOutputStream in Java

Before beginning an entry with putNextEntry(), we can set the compression method and level with setMethod() and setLevel(). The constants DEFLATED and STORED are the two legal values for setMethod(). If you use STORED, the entry is stored in the ZIP file without any compression.

If we use DEFLATED, you can also specify the compression speed/strength trade-off bypassing a number from 1 to 9 to setLevel(), where 9 gives the strongest and slowest level of compression. You can also use the constants Deflater.BEST_SPEED, Deflater.BEST_COMPRESSION, and Deflater.DEFAULT_COMPRESSION with the setLevel() method.

Note If we are simply storing an entry without compression, the ZIP file format requires that we specify, in advance, the entry size and CRC-32 checksum in the ZipEntry object for the entry. An exception is thrown if these values are not specified or are incorrectly specified.

The structure of the class ZipOutputStream is given by

public class java.util.zip.ZipOutputStream extends java.util.zip.DeflaterOutputStream{
// Public Constructor
public ZipOutputStream(OutputStream out);
// Constants
public static final int DEFLATED;
public static final int STORED;
// Public Instance Methods
public void close() throws IOException; // Overrides DeflaterOutputStream
public void closeEntry() throws IOException;
public void finish() throws IOException; // Overrides DeflaterOutputStream
public void putNextEntry(ZipEntry e) throws IOException;
public void setComment(String comment);
public void setLevel(int level);
public void setMethod(int method);
public synchronized void write(byte[] b, int off, int len) throws IOException; // Overrides DeflaterOutputStream
}

The details of the class structure are given as follows:

public static final int DEFLATED;

public static final int DEFLATED represents a constant that represents an entry is stored using the deflate algorithm.

public static final int STORED;

public static final int STORED represents a constant that represents a ZIP file entry is stored verbatim; in other words, with no compression applied.

public ZipOutputStream(OutputStream out);

public ZipOutputStream(OutputStream out) constructor creates a ZipOutputStream that writes compressed data to the given OutputStream.

READ  Class PipedWriter Class in Java

Parameter
out – The underlying output stream.

public void close();

public void close() method closes the 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. A subsequent entry can be started with putNextEntry().

public void finish();

public void finish() method finishes writing compressed data to the underlying stream without closing it.

public void putNextEntry(ZipEntry e);

public void putNextEntry(ZipEntry e) method writes the information in the given ZipEntry to the stream and positions the stream for the entry data. The actual entry data can then be written to the stream using write(). The default compression method and level are used if one is not specified for the entry. When all of the entry data has been written, use closeEntry() to finish the entry.

If this method is called when there is already an open entry, that entry is closed and this entry is opened.

Parameter
e – The new entry.

public void setComment(String comment);

public void setComment(String comment) method sets the comment string for this ZIP file.

Parameter
comment – The new comment string.

public void setLevel(int level);

public void setLevel(int level) method sets the default compression level of subsequent DEFLATED entries. The default value is Deflater.DEFAULT_COMPRESSION.

Parameter
level – A compression level, from 0 (NO_COMPRESSION) to 9 (BEST_COMPRESSION).

public void setMethod(int method);

public void setMethod(int method) method sets the default compression method of this ZipOutputStream for entries that do not specify a method.

Parameter
method – A compression method, either DEFLATED or STORED.

public synchronized void write(byte[] b, int off, int len);

public synchronized void write(byte[] b, int off, int len) method takes len bytes from the given buffer, starting at off, and compresses them. The method then writes the compressed data to the underlying OutputStream for the current entry. The method blocks until all the bytes have been written.

READ  Class Method in Java

Parameter
b – An array of bytes to write to the stream.
off – An offset into the byte array.
len – The number of bytes to write.

Apart from these ZipOutputStream class 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)

ZipOutputStream class also has inherited methods from FilterOutputStream. They are as follows:

  • flush()
  • write(byte[])

ZipOutputStream class also has inherited one method from DeflaterOutputStream. That is as follows:

  • write(int)

Share and Enjoy !

Leave a Comment

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