Top Description Inners Fields Constructors Methods
org.apache.tools.zip

public Class ZipOutputStream

extends FilterOutputStream
Class Inheritance
Static Imports
org.apache.tools.zip.ZipConstants.DATA_DESCRIPTOR_MIN_VERSION, .ZipConstants.DWORD, .ZipConstants.INITIAL_VERSION, .ZipConstants.SHORT, .ZipConstants.WORD, .ZipConstants.ZIP64_MAGIC, .ZipConstants.ZIP64_MAGIC_SHORT, .ZipConstants.ZIP64_MIN_VERSION, .ZipLong.putLong, .ZipShort.putShort

Reimplementation of 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.

Nested and Inner Type Summary

Modifier and TypeClass 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.

Field Summary

Modifier and TypeField 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.UnicodeExtraFieldPolicy
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<ZipEntry>
entries

List of ZipEntries written so far.

private ZipOutputStream.CurrentEntry
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<ZipEntry, Long>
offsets

Holds the offsets of the LFH starts for each entry.

private static final byte[]
private final byte[]
oneByte

Temporary buffer used for the write(int) method.

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.

Inherited from java.io.FilterOutputStream:
out

Constructor Summary

AccessConstructor and Description
public
ZipOutputStream(OutputStream
the outputstream to zip
out
)

Creates a new ZIP OutputStream filtering the underlying stream.

public
ZipOutputStream(File
the file to zip to
file
)

Creates a new ZIP OutputStream writing to a File.

Method Summary

Modifier and TypeMethod 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.
adjustToLong
(int
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:

boolean
canWriteEntryData
(ZipEntry
ZipEntry
ae
)

Whether this stream is able to write the given entry.

private boolean

Returns:

boolean
checkIfNeedsZip64
(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.

public void
close()

Overrides java.io.FilterOutputStream.close.

Implements java.io.Closeable.close.

Closes this output stream and releases any system resources associated with the stream.
public void
closeEntry()

Writes all necessary data for this entry.

private void
closeEntry(boolean actuallyNeedsZip64)

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[]
createLocalFileHeader(ZipEntry ze, ByteBuffer name, boolean encodable)

protected final void
deflate()

Writes next block of compressed data to the output stream.

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
finish()

Finish writing the archive.

public void
flush()

Overrides java.io.FilterOutputStream.flush.

Implements java.io.Flushable.flush.

Flushes this output stream and forces any buffered output bytes to be written out to the stream.
private void
flushDeflater()

Ensures all bytes sent to the deflater are written to the stream.

protected byte[]

Returns:

the bytes as a byte array
getBytes
(String
the string to get bytes from
name
)

Retrieve the bytes for the given String in the encoding set for this Stream.

private Zip64Mode

Returns:

Zip64Mode
getEffectiveZip64Mode
(ZipEntry
ZipEntry
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.
getEncoding
()

The encoding to use for filenames and the file comment.

private ZipEncoding
private GeneralPurposeBit
getGeneralPurposeBits(final int zipMethod, final boolean utfFallback)

private ByteBuffer
private Zip64ExtendedInformationExtraField

Returns:

Zip64ExtendedInformationExtraField
getZip64Extra
(ZipEntry
ZipEntry
ze
)

Get the existing ZIP64 extended information extra field or create a new one and add it to the entry.

private boolean

Returns:

boolean
handleSizesAndCrc
(long
long
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:

boolean
hasZip64Extra
(ZipEntry
ZipEntry
ze
)

Is there a ZIP64 extended information extra field for the entry?

private boolean
isDeflatedToOutputStream(int zipMethod)

public boolean

Returns:

true if seekable
isSeekable
()

This method indicates whether this archive is writing to a seekable stream (i.e., to a random access file).

private boolean
isTooLageForZip32(ZipEntry zipArchiveEntry)

private boolean
isZip64Required(ZipEntry entry1, Zip64Mode requestedMode)

private void
public void
putNextEntry(ZipEntry archiveEntry)

Put the specified entry into the archive.

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
setComment(String
the comment
comment
)

Set the file comment.

public void
setCreateUnicodeExtraFields(ZipOutputStream.UnicodeExtraFieldPolicy
boolean
b
)

Whether 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
setLevel(int
the compression level.
level
)

Sets the compression level for subsequent entries.

public void
setMethod(int
an int from java.util.zip.ZipEntry
method
)

Sets 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
setUseZip64(Zip64Mode
Zip64Mode
mode
)

Whether Zip64 extensions will be used.

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

Returns:

the date as a ZipLong
toDosTime
(Date
the Date to convert
time
)

Deprecated use ZipUtil#toDosTime
Convert a Date object to a DOS date/time field.
protected static byte[]

Returns:

the date as a byte array
toDosTime
(long
number 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
versionNeededToExtract(final int zipMethod, final boolean zip64)

public void
write(int
the byte to write
b
)

Overrides java.io.FilterOutputStream.write.

Writes a byte to ZIP entry.
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.io.FilterOutputStream.write.

Writes bytes to ZIP entry.
protected void
writeCentralDirectoryEnd()

Writes the "End of central dir record".

private void
protected void
writeCentralFileHeader(ZipEntry
the entry to write
ze
)

Writes the central file header entry.

private void
writeCounted(byte[]
the byte array to write
data
)

Write bytes to output or random access file.

private void
writeCounted(byte[] data, int offset, int length)

protected void
writeDataDescriptor(ZipEntry
the entry to write
ze
)

Writes the data descriptor entry.

private void
writeDeflated(byte[]
byte[]
b
,
int
int
offset
,
int
int
length
)

write implementation for DEFLATED entries.

protected void
writeLocalFileHeader(ZipEntry
the entry to write
ze
)

Writes the local file header entry

protected final void
writeOut(byte[]
the byte array to write
data
)

Write bytes to output or random access file.

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".

Inherited from java.io.FilterOutputStream:
write