java.util.zip

Class ZipOutputStream

public class ZipOutputStream extends DeflaterOutputStream implements ZipConstants

This is a FilterOutputStream that writes the files into a zip archive one after another. It has a special method to start a new zip entry. The zip entries contains information about the file name size, compressed size, CRC, etc. It includes support for STORED and DEFLATED entries. This class is not thread safe.
Field Summary
static intDEFLATED
Compression method.
static intSTORED
Compression method.
Constructor Summary
ZipOutputStream(OutputStream out)
Creates a new Zip output stream, writing a zip archive.
Method Summary
voidcloseEntry()
Closes the current entry.
voidfinish()
Finishes the stream.
voidputNextEntry(ZipEntry entry)
Starts a new Zip entry.
voidsetComment(String comment)
Set the zip file comment.
voidsetLevel(int level)
Sets default compression level.
voidsetMethod(int method)
Sets default compression method.
voidwrite(byte[] b, int off, int len)
Writes the given buffer to the current entry.

Field Detail

DEFLATED

public static final int DEFLATED
Compression method. This method uses the Deflater.

STORED

public static final int STORED
Compression method. This method doesn't compress at all.

Constructor Detail

ZipOutputStream

public ZipOutputStream(OutputStream out)
Creates a new Zip output stream, writing a zip archive.

Parameters: out the output stream to which the zip archive is written.

Method Detail

closeEntry

public void closeEntry()
Closes the current entry.

Throws: IOException if an I/O error occured. ZipException if no entry is active.

finish

public void finish()
Finishes the stream. This will write the central directory at the end of the zip file and flush the stream.

Throws: IOException if an I/O error occured.

putNextEntry

public void putNextEntry(ZipEntry entry)
Starts a new Zip entry. It automatically closes the previous entry if present. If the compression method is stored, the entry must have a valid size and crc, otherwise all elements (except name) are optional, but must be correct if present. If the time is not set in the entry, the current time is used.

Parameters: entry the entry.

Throws: IOException if an I/O error occured. ZipException if stream was finished.

setComment

public void setComment(String comment)
Set the zip file comment.

Parameters: comment the comment.

Throws: IllegalArgumentException if encoding of comment is longer than 0xffff bytes.

setLevel

public void setLevel(int level)
Sets default compression level. The new level will be activated immediately.

Throws: IllegalArgumentException if level is not supported.

See Also: Deflater

setMethod

public void setMethod(int method)
Sets default compression method. If the Zip entry specifies another method its method takes precedence.

Parameters: method the method.

Throws: IllegalArgumentException if method is not supported.

See Also: STORED DEFLATED

write

public void write(byte[] b, int off, int len)
Writes the given buffer to the current entry.

Throws: IOException if an I/O error occured. ZipException if no entry is active.