public class ZipOutput
extends java.lang.Object
Multiple entries and output streams can be open at the same time. The entries are added to the central directory in the order in which they are opened, but the corresponding data are only written when their output streams are closed.
The code automatically computes the CRC and lengths of the data, for compressed and uncompressed data.
Modifier and Type | Class and Description |
---|---|
static class |
ZipOutput.LargeDataOutputStream
This output stream is mostly identical to DataOutputStream, except it stores the amount of
bytes written so far in a long instead of an int.
|
Modifier and Type | Field and Description |
---|---|
protected ZipOutput.LargeDataOutputStream |
outputStream |
Constructor and Description |
---|
ZipOutput(java.io.OutputStream outputStream)
Creates a new ZipOutput.
|
ZipOutput(java.io.OutputStream outputStream,
boolean useZip64)
Creates a new ZipOutput.
|
ZipOutput(java.io.OutputStream outputStream,
boolean useZip64,
int uncompressedAlignment)
Creates a new ZipOutput that aligns uncompressed entries.
|
ZipOutput(java.io.OutputStream outputStream,
byte[] header,
int uncompressedAlignment,
boolean useZip64)
Creates a new ZipOutput that aligns uncompressed entries.
|
ZipOutput(java.io.OutputStream outputStream,
byte[] header,
int uncompressedAlignment,
boolean useZip64,
java.lang.String comment)
Creates a new ZipOutput that aligns uncompressed entries and contains a comment.
|
ZipOutput(java.io.OutputStream outputStream,
int uncompressedAlignment)
Creates a new ZipOutput that aligns uncompressed entries.
|
ZipOutput(java.io.OutputStream outputStream,
int uncompressedAlignment,
boolean useZip64,
java.lang.String comment)
Creates a new ZipOutput that aligns uncompressed entries and contains a comment.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes the zip archive, writing out its central directory and closing the underlying output
stream.
|
void |
close(long centralDirectoryOffset)
Closes the zip archive, writing out its central directory and closing the underlying output
stream.
|
java.io.OutputStream |
createOutputStream(java.lang.String name,
boolean compress,
int modificationTime)
Creates a new zip entry, returning an output stream to write its data.
|
java.io.OutputStream |
createOutputStream(java.lang.String name,
boolean compress,
int uncompressedAlignment,
int modificationTime)
Creates a new zip entry, returning an output stream to write its data.
|
java.io.OutputStream |
createOutputStream(java.lang.String name,
boolean compress,
int uncompressedAlignment,
int modificationTime,
byte[] extraField,
java.lang.String comment)
Creates a new zip entry, returning an output stream to write its data.
|
static void |
main(java.lang.String[] args)
Provides a simple test for this class, creating a zip file with the given name and a few
aligned/compressed/uncompressed zip entries.
|
protected long |
size()
Returns the current size of the data written to the output stream.
|
protected void |
writeEndOfCentralDirectory(long centralDirectoryOffset,
long centralDirectorySize)
Ends the central directory.
|
protected long |
writeEntriesOfCentralDirectory()
Writes the zip entries in the central directory.
|
protected void |
writeInt(int value)
Writes out a little-endian int value to the zip output stream.
|
protected void |
writeInt(long value)
Writes out a little-endian int value to the zip output stream.
|
protected void |
writeLong(long value)
Writes out a little-endian long value to the zip output stream.
|
protected void |
writeShort(int value)
Writes out a little-endian short value to the zip output stream.
|
protected long |
writeStartOfCentralDirectory()
Starts the central directory.
|
protected void |
writeZip64EndOfCentralDirectory(long centralDirectoryOffset,
long centralDirectorySize)
Writes out a ZIP64 end of central directory entry.
|
protected void |
writeZip64EndOfCentralDirectoryLocator(long zip64CentralDirectoryOffset)
Writes out a ZIP64 end of central directory locator.
|
protected ZipOutput.LargeDataOutputStream outputStream
public ZipOutput(java.io.OutputStream outputStream)
outputStream
- the output stream to which the zip data will be written.public ZipOutput(java.io.OutputStream outputStream, boolean useZip64)
outputStream
- the output stream to which the zip data will be written.useZip64
- Whether to write out the archive in zip64 format.public ZipOutput(java.io.OutputStream outputStream, int uncompressedAlignment)
outputStream
- the output stream to which the zip data will be written.uncompressedAlignment
- the default alignment of uncompressed data.public ZipOutput(java.io.OutputStream outputStream, boolean useZip64, int uncompressedAlignment)
outputStream
- the output stream to which the zip data will be written.useZip64
- Whether to write out the archive in zip64 format.uncompressedAlignment
- the default alignment of uncompressed data.public ZipOutput(java.io.OutputStream outputStream, int uncompressedAlignment, boolean useZip64, java.lang.String comment)
outputStream
- the output stream to which the zip data will be written.uncompressedAlignment
- the default alignment of uncompressed data.useZip64
- Whether to write out the archive in zip64 format.comment
- optional comment for the entire zip file.public ZipOutput(java.io.OutputStream outputStream, byte[] header, int uncompressedAlignment, boolean useZip64) throws java.io.IOException
outputStream
- the output stream to which the zip data will be written.header
- an optional header for the zip file.useZip64
- Whether to write out the archive in zip64 format.uncompressedAlignment
- the requested alignment of uncompressed data.java.io.IOException
public ZipOutput(java.io.OutputStream outputStream, byte[] header, int uncompressedAlignment, boolean useZip64, java.lang.String comment) throws java.io.IOException
outputStream
- the output stream to which the zip data will be written.header
- an optional header for the zip file.uncompressedAlignment
- the requested alignment of uncompressed data.useZip64
- Whether to write out the archive in zip64 format.comment
- optional comment for the entire zip file.java.io.IOException
public java.io.OutputStream createOutputStream(java.lang.String name, boolean compress, int modificationTime) throws java.io.IOException
name
- the name of the zip entry.compress
- specifies whether the entry should be compressed with the default alignment.modificationTime
- the modification date and time of the zip entry, in DOS format.java.io.IOException
public java.io.OutputStream createOutputStream(java.lang.String name, boolean compress, int uncompressedAlignment, int modificationTime) throws java.io.IOException
name
- the name of the zip entry.compress
- specifies whether the entry should be compressed.uncompressedAlignment
- the requested alignment of uncompressed data.modificationTime
- the modification date and time of the zip entry, in DOS format.java.io.IOException
public java.io.OutputStream createOutputStream(java.lang.String name, boolean compress, int uncompressedAlignment, int modificationTime, byte[] extraField, java.lang.String comment) throws java.io.IOException
name
- the name of the zip entry.compress
- specifies whether the entry should be compressed.uncompressedAlignment
- the requested alignment of uncompressed data.modificationTime
- the modification date and time of the zip entry, in DOS format.extraField
- optional extra field data. These should contain chunks, each with a short ID,
a short length (little endian), and their corresponding data. The IDs 0-31 are reserved for
Pkware. Java's jar tool just specifies an ID 0xcafe on its first entry.comment
- an optional comment.java.io.IOException
public void close() throws java.io.IOException
java.io.IOException
public void close(long centralDirectoryOffset) throws java.io.IOException
java.io.IOException
protected long size()
protected long writeStartOfCentralDirectory()
protected long writeEntriesOfCentralDirectory() throws java.io.IOException
java.io.IOException
protected void writeEndOfCentralDirectory(long centralDirectoryOffset, long centralDirectorySize) throws java.io.IOException
centralDirectoryOffset
- the offset of the central directory.centralDirectorySize
- the size of the central directory, not counting the empty header
and the trailer.java.io.IOException
protected void writeZip64EndOfCentralDirectory(long centralDirectoryOffset, long centralDirectorySize) throws java.io.IOException
centralDirectoryOffset
- The offset from the start of the archive to the first central
directory record.centralDirectorySize
- The total size of all central directory records.java.io.IOException
- In case a problem occurs during writing.protected void writeZip64EndOfCentralDirectoryLocator(long zip64CentralDirectoryOffset) throws java.io.IOException
zip64CentralDirectoryOffset
- The offset to the zip64 end of central directory record.java.io.IOException
- In case a problem occurs during writing.protected void writeShort(int value) throws java.io.IOException
java.io.IOException
protected void writeInt(int value) throws java.io.IOException
java.io.IOException
protected void writeInt(long value) throws java.io.IOException
java.io.IOException
protected void writeLong(long value) throws java.io.IOException
java.io.IOException
public static void main(java.lang.String[] args)