Top Description Inners Fields Constructors Methods
org.apache.commons.compress.archivers.zip

public Class ZipArchiveOutputStream

extends ArchiveOutputStream<ZipArchiveEntry>
Class Inheritance
Known Direct Subclasses
org.apache.commons.compress.archivers.jar.JarArchiveOutputStream
Imports
java.io.ByteArrayOutputStream, .File, .IOException, .InputStream, .OutputStream, java.nio.ByteBuffer, java.nio.channels.SeekableByteChannel, java.nio.file.LinkOption, .OpenOption, .Path, java.util.HashMap, .LinkedList, .List, .Map, java.util.zip.Deflater, .ZipException, org.apache.commons.compress.archivers.ArchiveEntry, .ArchiveOutputStream, org.apache.commons.compress.utils.ByteUtils, .CharsetNames

Reimplementation of java.util.zip.ZipOutputStream to 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 java.nio.channels.SeekableByteChannel when it knows that the output is going to go to a file and no split archive shall be created.

If SeekableByteChannel cannot be used, this implementation will use a Data Descriptor to store size and CRC information for DEFLATED entries, you don't need to calculate them yourself. Unfortunately, this is not possible for the STORED method, where setting the CRC and uncompressed size information is required before putArchiveEntry(ZipArchiveEntry) can be called.

As of Apache Commons Compress 1.3, the class transparently supports Zip64 extensions and thus individual entries and archives larger than 4 GB or with more than 65,536 entries in most cases but explicit control is provided via setUseZip64. If the stream can not use SeekableByteChannel and you try to write a ZipArchiveEntry of unknown size, then Zip64 extensions will be disabled by default.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
ZipArchiveOutputStream.CurrentEntry

Structure collecting information for the entry that is currently being written.

private static class
public static class
ZipArchiveOutputStream.UnicodeExtraFieldPolicy

enum that represents the possible policies for creating Unicode extra fields.

Field Summary

Modifier and TypeField and Description
pack-priv static final int
private long
cdDiskNumberStart

Disk number start of central directory.

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
pack-priv 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
charsetName

The encoding to use for file names and the file comment.

private String
comment

The file comment.

private final byte[]
private ZipArchiveOutputStream.UnicodeExtraFieldPolicy
createUnicodeExtraFields

whether to create UnicodePathExtraField-s for each entry.

pack-priv 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.

public static final int
EFS_FLAG

General purpose flag, which indicates that file names are written in UTF-8.

private final List<ZipArchiveEntry>
entries

List of ZipArchiveEntries written so far.

private ZipArchiveOutputStream.CurrentEntry
entry

Current entry.

pack-priv static final byte[]
EOCD_SIG

end of central dir signature

private long
eocdLength

Length of end of central directory

private boolean
fallbackToUTF8

Whether to encode non-encodable file names as UTF-8.

protected 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 final boolean
isSplitZip

Whether we are creating a split zip

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
pack-priv 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 final Map<ZipArchiveEntry, ZipArchiveOutputStream.EntryMetaData>
metaData

Holds some book-keeping data for each entry.

private int
method

Default compression method for next entry.

private final Map<Integer, Integer>
numberOfCDInDiskData

Holds the number of Central Directories on each disk, this is used when writing Zip64 End Of Central Directory and End Of Central Directory

private static final byte[]
private final OutputStream
public static final int
STORED

Compression method for stored entries.

private final StreamCompressor
private boolean
useUTF8Flag

whether to use the general purpose bit flag when writing UTF-8 file names or not.

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 file names and the file comment.

Constructor Summary

AccessConstructor and Description
public
ZipArchiveOutputStream(final File
the file to ZIP to
file
)

Creates a new ZIP OutputStream writing to a File.

public
ZipArchiveOutputStream(final File
the file that will become the last part of the split archive
file
,
final long
maximum size of a single part of the split archive created by this stream. Must be between 64kB and about 4GB.
zipSplitSize
)

Creates a split ZIP Archive.

public
ZipArchiveOutputStream(final OutputStream
the outputstream to zip
out
)

Creates a new ZIP OutputStream filtering the underlying stream.

public
ZipArchiveOutputStream(final Path
the path to the file that will become the last part of the split archive
path
,
final long
maximum size of a single part of the split archive created by this stream. Must be between 64kB and about 4GB.
zipSplitSize
)

Creates a split ZIP Archive.

public
ZipArchiveOutputStream(final Path
the file to ZIP to
file
,
final OpenOption...
options specifying how the file is opened.
options
)

Creates a new ZIP OutputStream writing to a Path.

public
ZipArchiveOutputStream(final SeekableByteChannel
the channel to ZIP to
channel
)

Creates a new ZIP OutputStream writing to a SeekableByteChannel.

Method Summary

Modifier and TypeMethod and Description
public void
addRawArchiveEntry(final ZipArchiveEntry
The archive entry to add
entry
,
final InputStream
The raw input stream of a different entry. May be compressed/encrypted.
rawStream
)

Adds an archive entry with a raw input stream.

private void
addUnicodeExtraFields(final ZipArchiveEntry ze, final boolean encodable, final ByteBuffer name)

Adds UnicodeExtra fields for name and file comment if mode is ALWAYS or the data cannot be encoded using the configured encoding.

public boolean
canWriteEntryData(final ArchiveEntry
the entry to test
ae
)

Overrides org.apache.commons.compress.archivers.ArchiveOutputStream.canWriteEntryData.

Whether this stream is able to write the given entry.
private boolean
checkIfNeedsZip64(final Zip64Mode effectiveMode)

Verifies the sizes aren'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.OutputStream.close.

Implements java.io.Closeable.close.

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

Implements abstract org.apache.commons.compress.archivers.ArchiveOutputStream.closeArchiveEntry.

Writes all necessary data for this entry.
private void
closeCopiedEntry(final boolean
This entry is second phase of a 2-phase ZIP creation, size, compressed size and crc are known in ZipArchiveEntry
phased
)

Writes all necessary data for this entry.

private void
closeEntry(final boolean actuallyNeedsZip64, final boolean phased)

private void
public ZipArchiveEntry
createArchiveEntry(final File
the file to create the entry from
inputFile
,
final String
name to use for the entry
entryName
)

Implements abstract org.apache.commons.compress.archivers.ArchiveOutputStream.createArchiveEntry.

Creates a new ZIP entry taking some information from the given file and using the provided name.
public ZipArchiveEntry

Returns:

a new instance.
createArchiveEntry
(final Path
path to create the entry from.
inputPath
,
final String
name of the entry.
entryName
,
final LinkOption...
options indicating how symbolic links are handled.
options
)

Overrides org.apache.commons.compress.archivers.ArchiveOutputStream.createArchiveEntry.

Creates a new ZIP entry taking some information from the given file and using the provided name.
private byte[]
private byte[]
createCentralFileHeader(final ZipArchiveEntry
the entry to write
ze
,
final ByteBuffer
The encoded name
name
,
final ZipArchiveOutputStream.EntryMetaData
meta data for this file
entryMetaData
,
final boolean needsZip64Extra)

Writes the central file header entry.

private byte[]
createLocalFileHeader(final ZipArchiveEntry ze, final ByteBuffer name, final boolean encodable, final boolean phased, final long archiveOffset)

protected final void
deflate()

Writes next block of compressed data to the output stream.

pack-priv void
destroy()

Closes the underlying stream/file without finishing the archive, the result will likely be a corrupt archive.

public void
finish()

Implements abstract org.apache.commons.compress.archivers.ArchiveOutputStream.finish.

Finishes the addition of entries to this stream, without closing it.
public void
flush()

Overrides java.io.OutputStream.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.

public long

Returns:

the number of written bytes
getBytesWritten
()

Overrides org.apache.commons.compress.archivers.ArchiveOutputStream.getBytesWritten.

Returns the total number of bytes written to this stream.
private Zip64Mode
getEffectiveZip64Mode(final ZipArchiveEntry ze)

If the mode is AsNeeded and the entry is a compressed entry of unknown size that gets written to a non-seekable stream then change the default to Never.

public String

Returns:

null if using the platform's default character encoding.
getEncoding
()

The encoding to use for file names and the file comment.

private ZipEncoding
private GeneralPurposeBit
getGeneralPurposeBits(final boolean utfFallback, final boolean usesDataDescriptor)

private ByteBuffer
private Zip64ExtendedInformationExtraField
getZip64Extra(final ZipArchiveEntry ze)

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

private boolean
handleSizesAndCrc(final long bytesWritten, final long crc, final 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(final ZipArchiveEntry ze, final long lfhOffset, final boolean needsZip64Extra)

If the entry needs Zip64 extra information inside the central directory then configure its data.

private boolean
hasZip64Extra(final ZipArchiveEntry ze)

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

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
isTooLargeForZip32(final ZipArchiveEntry zipArchiveEntry)

private boolean
isZip64Required(final ZipArchiveEntry entry1, final Zip64Mode requestedMode)

private void
public void
putArchiveEntry(final ZipArchiveEntry
describes the entry
archiveEntry
)

Implements abstract org.apache.commons.compress.archivers.ArchiveOutputStream.putArchiveEntry.

Writes the headers for an archive entry to the output stream.
private void
putArchiveEntry(final ZipArchiveEntry
The archiveEntry
archiveEntry
,
final boolean
If true size, compressedSize and crc required to be known up-front in the archiveEntry
phased
)

Writes the headers for an archive entry to the output stream.

private void
rewriteSizesAndCrc(final 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(final String
the comment
comment
)

Sets the file comment.

public void
setCreateUnicodeExtraFields(final ZipArchiveOutputStream.UnicodeExtraFieldPolicy
whether to create Unicode Extra Fields.
b
)

Whether to create Unicode Extra Fields.

private void
setDefaults(final ZipArchiveEntry entry)

Provides default values for compression method and last modification time.

public void
setEncoding(final String
the encoding to use for file names, use null for the platform's default encoding
encoding
)

The encoding to use for file names and the file comment.

public void
setFallbackToUTF8(final boolean
whether to fall back to UTF and the language encoding flag if the file name cannot be encoded using the specified encoding.
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(final int
the compression level.
level
)

Sets the compression level for subsequent entries.

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

Sets the default compression method for subsequent entries.

public void
setUseLanguageEncodingFlag(final boolean
whether to set the language encoding flag if the file name encoding is UTF-8
b
)

Whether to set the language encoding flag if the file name encoding is UTF-8.

public void
setUseZip64(final Zip64Mode
Whether Zip64 extensions will be used.
mode
)

Whether Zip64 extensions will be used.

private boolean
shouldAddZip64Extra(final ZipArchiveEntry entry, final Zip64Mode mode)

Whether to add a Zip64 extended information extra field to the local file header.

private boolean

Returns:

true if zip64 End Of Central Directory is needed
shouldUseZip64EOCD
()

4.4.1.4 If one of the fields in the end of central directory record is too small to hold required data, the field SHOULD be set to -1 (0xFFFF or 0xFFFFFFFF) and the ZIP64 format record SHOULD be created.

private boolean
usesDataDescriptor(final int zipMethod, final boolean phased)

private void
validateIfZip64IsNeededInEOCD()

If the Zip64 mode is set to never, then all the data in End Of Central Directory should not exceed their limits.

private void
validateSizeInformation(final 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, final boolean usedDataDescriptor)

private int
versionNeededToExtractMethod(final int zipMethod)

public void
write(final byte[]
the byte array to write
b
,
final int
the start position to write from
offset
,
final int
the number of bytes to write
length
)

Overrides java.io.OutputStream.write.

Writes bytes to ZIP entry.
protected void
writeCentralDirectoryEnd()

Writes the "End of central dir record".

private void
protected void
writeCentralFileHeader(final ZipArchiveEntry
the entry to write
ze
)

Writes the central file header entry.

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

Write bytes to output or random access file.

protected void
writeDataDescriptor(final ZipArchiveEntry
the entry to write
ze
)

Writes the data descriptor entry.

protected void
writeLocalFileHeader(final ZipArchiveEntry
the entry to write
ze
)

Writes the local file header entry

private void
writeLocalFileHeader(final ZipArchiveEntry ze, final boolean phased)

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

Write bytes to output or random access file.

protected final void
writeOut(final byte[]
the byte array to write
data
,
final int
the start position to write from
offset
,
final int
the number of bytes to write
length
)

Write bytes to output or random access file.

public void
writePreamble(final byte[]
data to write
preamble
)

Write preamble data.

public void
writePreamble(final byte[]
data to write
preamble
,
final int
the start offset in the data
offset
,
final int
the number of bytes to write
length
)

Write preamble data.

protected void
writeZip64CentralDirectory()

Writes the "ZIP64 End of central dir record" and "ZIP64 End of central dir locator".

Inherited from org.apache.commons.compress.archivers.ArchiveOutputStream:
countcountgetCountwrite