java.util.zip.ZipOutputStream that does handle the extended
functionality of this package, especially internal/external file
attributes and extra fields with different layouts for local file
data and central directory entries.
This class will try to use RandomAccessFile when you know that the output is going to go to a
file.
If RandomAccessFile cannot be used, this implementation will use
a Data Descriptor to store size and CRC information for DEFLATED entries, this means, you don't need to
calculate them yourself. Unfortunately this is not possible for
the STORED method, here setting the CRC and
uncompressed size information is required before putNextEntry can be called.
As of Apache Ant 1.9.0 it transparently supports Zip64
extensions and thus individual entries and archives larger than 4
GB or with more than 65536 entries in most cases but explicit
control is provided via setUseZip64. If the stream can not
user RandomAccessFile and you try to write a ZipEntry of
unknown size then Zip64 extensions will be disabled by default.
| Modifier and Type | Class and Description |
|---|---|
| private static class | ZipOutputStream.CurrentEntry
Structure collecting information for the entry that is currently being written. |
| public static class | ZipOutputStream.UnicodeExtraFieldPolicy
enum that represents the possible policies for creating Unicode extra fields. |
| Modifier and Type | Field and Description |
|---|---|
| protected byte[] | buf
This buffer serves as a Deflater. |
| private static final int | |
| private final Calendar | |
| private long | cdLength
Length of central directory. |
| private long | cdOffset
Start of central directory. |
| private static final int | |
| private static final int | |
| private static final int | |
| private static final int | |
| private static final int | |
| private static final int | |
| private static final int | |
| private static final int | |
| private static final int | |
| private static final int | |
| private static final int | |
| private static final int | |
| private static final int | |
| protected static final byte[] | CFH_SIG
central file header signature |
| private static final int | |
| private static final int | |
| private static final int | |
| private static final int | |
| private String | comment
The file comment. |
| private final CRC32 | crc
CRC instance to avoid parsing DEFLATED data twice. |
| private ZipOutputStream. | createUnicodeExtraFields
whether to create UnicodePathExtraField-s for each entry. |
| protected static final byte[] | DD_SIG
data descriptor signature |
| protected final Deflater | def
This Deflater object is used for output. |
| public static final int | DEFAULT_COMPRESSION
Default compression level for deflated entries. |
| pack-priv static final String | DEFAULT_ENCODING
default encoding for file names and comment. |
| public static final int | DEFLATED
Compression method for deflated entries. |
| private static final int | |
| public static final int | EFS_FLAG
General purpose flag, which indicates that filenames are written in utf-8. |
| private static final byte[] | |
| private String | encoding
The encoding to use for filenames and the file comment. |
| private final List | entries
List of ZipEntries written so far. |
| private ZipOutputStream. | entry
Current entry. |
| protected static final byte[] | EOCD_SIG
end of central dir signature |
| private boolean | fallbackToUTF8
Whether to encode non-encodable file names as UTF-8. |
| private boolean | finished
indicates if this archive is finished. |
| private boolean | hasCompressionLevelChanged
Has the compression level changed when compared to the last entry? |
| private boolean | hasUsedZip64
Whether anything inside this archive has used a ZIP64 feature. |
| private int | level
Compression level for next entry. |
| private static final int | |
| private static final int | |
| private static final int | |
| private static final int | |
| private static final int | |
| private static final int | |
| private static final int | |
| private static final int | |
| protected static final byte[] | LFH_SIG
local file header signature |
| private static final int | |
| private static final int | |
| private static final int | |
| private static final byte[] | LZERO
Helper, a 0 as ZipLong. |
| private int | method
Default compression method for next entry. |
| private final Map | offsets
Holds the offsets of the LFH starts for each entry. |
| private static final byte[] | |
| private final byte[] | oneByte
Temporary buffer used for the |
| private final RandomAccessFile | raf
Optional random access output. |
| public static final int | STORED
Compression method for stored entries. |
| private boolean | useUTF8Flag
whether to use the general purpose bit flag when writing UTF-8 filenames or not. |
| private long | written
Count the bytes written to out. |
| private static final byte[] | ZERO
Helper, a 0 as ZipShort. |
| pack-priv static final byte[] | ZIP64_EOCD_LOC_SIG
ZIP64 end of central dir locator signature |
| pack-priv static final byte[] | ZIP64_EOCD_SIG
ZIP64 end of central dir signature |
| private Zip64Mode | |
| private ZipEncoding | zipEncoding
The zip encoding to use for filenames and the file comment. |
| Access | Constructor and Description |
|---|---|
| public | ZipOutputStream(OutputStream
the outputstream to zip out)Creates a new ZIP OutputStream filtering the underlying stream. |
| public |
| Modifier and Type | Method and Description |
|---|---|
| private void | addUnicodeExtraFields(ZipEntry
ZipEntry ze, boolean boolean encodable, ByteBuffer ByteBuffer name)Adds UnicodeExtra fields for name and file comment if mode is ALWAYS or the data cannot be encoded using the configured encoding. |
| protected static long | Returns: the unsigned int as a long.the value to treat as unsigned int. i)
Deprecated
use ZipUtil#adjustToLong
Assumes a negative integer really is a positive integer that
has wrapped around and re-creates the original value.
|
| public boolean | Returns: booleanZipEntry ae)Whether this stream is able to write the given entry. |
| private boolean | Returns: booleanZip64Mode effectiveMode)Ensures the current entry's size and CRC information is set to the values just written, verifies it isn't too big in the Zip64Mode.Never case and returns whether the entry would require a Zip64 extra field. |
| public void | close()
Overrides java. Implements java. |
| public void | |
| private void | |
| private byte[] | |
| private byte[] | createCentralFileHeader(ZipEntry
the entry to write ze, ByteBuffer The encoded name name, long Local file header offset for this file lfhOffset, boolean needsZip64Extra)Writes the central file header entry. |
| private byte[] | |
| protected final void | |
| private void | |
| pack-priv void | destroy()
Closes the underlying stream/file without finishing the archive, the result will likely be a corrupt archive. |
| public void | |
| public void | flush()
Overrides java. Implements java. |
| private void | |
| protected byte[] | |
| private Zip64Mode | Returns: Zip64ModeZipEntry ze)If the mode is AsNeeded and the entry is a compressed entry of unknown size that gets written to a non-seekable stream the change the default to Never. |
| public String | Returns: null if using the platform's default character encoding.The encoding to use for filenames and the file comment. |
| private ZipEncoding | |
| private GeneralPurposeBit | |
| private ByteBuffer | |
| private Zip64ExtendedInformationExtraField | Returns: Zip64ExtendedInformationExtraFieldZipEntry ze)Get the existing ZIP64 extended information extra field or create a new one and add it to the entry. |
| private boolean | Returns: booleanlong bytesWritten, long long crc, Zip64Mode Zip64Mode effectiveMode)Ensures the current entry's size and CRC information is set to the values just written, verifies it isn't too big in the Zip64Mode.Never case and returns whether the entry would require a Zip64 extra field. |
| private void | handleZip64Extra(ZipEntry
ZipEntry ze, long long lfhOffset, boolean boolean needsZip64Extra)If the entry needs Zip64 extra information inside the central directory then configure its data. |
| private boolean | Returns: booleanZipEntry ze)Is there a ZIP64 extended information extra field for the entry? |
| private boolean | |
| public boolean | Returns: true if seekableThis method indicates whether this archive is writing to a seekable stream (i.e., to a random access file). |
| private boolean | |
| private boolean | |
| private void | |
| public void | |
| private void | rewriteSizesAndCrc(boolean
boolean actuallyNeedsZip64)When using random access output, write the local file header and potentially the ZIP64 extra containing the correct CRC and compressed/uncompressed sizes. |
| public void | |
| public void | setCreateUnicodeExtraFields(ZipOutputStream.
boolean bWhether to create Unicode Extra Fields. |
| private void | setDefaults(ZipEntry
ZipEntry entry)Provides default values for compression method and last modification time. |
| public void | setEncoding(final String
the encoding value encoding)The encoding to use for filenames and the file comment. |
| public void | setFallbackToUTF8(boolean
boolean b)Whether to fall back to UTF and the language encoding flag if the file name cannot be encoded using the specified encoding. |
| public void | |
| public void | setMethod(int
an method)int from java.util.zip.ZipEntrySets the default compression method for subsequent entries. |
| public void | setUseLanguageEncodingFlag(boolean
boolean b)Whether to set the language encoding flag if the file name encoding is UTF-8. |
| public void | |
| private boolean | shouldAddZip64Extra(ZipEntry
ZipEntry entry, Zip64Mode Zip64Mode mode)Whether to add a Zip64 extended information extra field to the local file header. |
| protected static ZipLong | |
| protected static byte[] | Returns: the date as a byte arraynumber of milliseconds since the epoch t)
Deprecated
use ZipUtil#toDosTime
Convert a Date object to a DOS date/time field.
|
| private void | validateSizeInformation(Zip64Mode
Zip64Mode effectiveMode)Throws an exception if the size is unknown for a stored entry that is written to a non-seekable output or the entry is too big to be written without Zip64 extra but the mode has been set to Never. |
| private int | |
| public void | write(int
the byte to write b)Overrides java. |
| public void | write(byte[]
the byte array to write b, int the start position to write from offset, int the number of bytes to write length)Overrides java. |
| protected void | |
| private void | |
| protected void | |
| private void | |
| private void | |
| protected void | |
| private void | writeDeflated(byte[]
byte[] b, int int offset, int int length)write implementation for DEFLATED entries. |
| protected void | |
| protected final void | |
| protected final void | writeOut(byte[]
the byte array to write data, int the start position to write from offset, int the number of bytes to write length)Write bytes to output or random access file. |
| protected void | writeZip64CentralDirectory()
Writes the "ZIP64 End of central dir record" and "ZIP64 End of central dir locator". |