Class ZipArchiveOutputStream

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.AutoCloseable
    Direct Known Subclasses:
    JarArchiveOutputStream

    public class ZipArchiveOutputStream
    extends ArchiveOutputStream
    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 SeekableByteChannel when it knows that the output is going to go to a file.

    If SeekableByteChannel 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 putArchiveEntry(ArchiveEntry) can be called.

    As of Apache Commons Compress 1.3 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(org.apache.commons.compress.archivers.zip.Zip64Mode). 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.

    • Field Detail

      • LFH_VERSION_NEEDED_OFFSET

        private static final int LFH_VERSION_NEEDED_OFFSET
        See Also:
        Constant Field Values
      • LFH_COMPRESSED_SIZE_OFFSET

        private static final int LFH_COMPRESSED_SIZE_OFFSET
        See Also:
        Constant Field Values
      • LFH_ORIGINAL_SIZE_OFFSET

        private static final int LFH_ORIGINAL_SIZE_OFFSET
        See Also:
        Constant Field Values
      • LFH_FILENAME_LENGTH_OFFSET

        private static final int LFH_FILENAME_LENGTH_OFFSET
        See Also:
        Constant Field Values
      • LFH_EXTRA_LENGTH_OFFSET

        private static final int LFH_EXTRA_LENGTH_OFFSET
        See Also:
        Constant Field Values
      • CFH_VERSION_MADE_BY_OFFSET

        private static final int CFH_VERSION_MADE_BY_OFFSET
        See Also:
        Constant Field Values
      • CFH_VERSION_NEEDED_OFFSET

        private static final int CFH_VERSION_NEEDED_OFFSET
        See Also:
        Constant Field Values
      • CFH_COMPRESSED_SIZE_OFFSET

        private static final int CFH_COMPRESSED_SIZE_OFFSET
        See Also:
        Constant Field Values
      • CFH_ORIGINAL_SIZE_OFFSET

        private static final int CFH_ORIGINAL_SIZE_OFFSET
        See Also:
        Constant Field Values
      • CFH_FILENAME_LENGTH_OFFSET

        private static final int CFH_FILENAME_LENGTH_OFFSET
        See Also:
        Constant Field Values
      • CFH_EXTRA_LENGTH_OFFSET

        private static final int CFH_EXTRA_LENGTH_OFFSET
        See Also:
        Constant Field Values
      • CFH_COMMENT_LENGTH_OFFSET

        private static final int CFH_COMMENT_LENGTH_OFFSET
        See Also:
        Constant Field Values
      • CFH_DISK_NUMBER_OFFSET

        private static final int CFH_DISK_NUMBER_OFFSET
        See Also:
        Constant Field Values
      • CFH_INTERNAL_ATTRIBUTES_OFFSET

        private static final int CFH_INTERNAL_ATTRIBUTES_OFFSET
        See Also:
        Constant Field Values
      • CFH_EXTERNAL_ATTRIBUTES_OFFSET

        private static final int CFH_EXTERNAL_ATTRIBUTES_OFFSET
        See Also:
        Constant Field Values
      • finished

        protected boolean finished
        indicates if this archive is finished. protected for use in Jar implementation
      • DEFLATED

        public static final int DEFLATED
        Compression method for deflated entries.
        See Also:
        Constant Field Values
      • DEFAULT_COMPRESSION

        public static final int DEFAULT_COMPRESSION
        Default compression level for deflated entries.
        See Also:
        Constant Field Values
      • STORED

        public static final int STORED
        Compression method for stored entries.
        See Also:
        Constant Field Values
      • DEFAULT_ENCODING

        static final java.lang.String DEFAULT_ENCODING
        default encoding for file names and comment.
        See Also:
        Constant Field Values
      • EMPTY

        private static final byte[] EMPTY
      • comment

        private java.lang.String comment
        The file comment.
      • level

        private int level
        Compression level for next entry.
      • hasCompressionLevelChanged

        private boolean hasCompressionLevelChanged
        Has the compression level changed when compared to the last entry?
      • method

        private int method
        Default compression method for next entry.
      • entries

        private final java.util.List<ZipArchiveEntry> entries
        List of ZipArchiveEntries written so far.
      • cdOffset

        private long cdOffset
        Start of central directory.
      • cdLength

        private long cdLength
        Length of central directory.
      • ZERO

        private static final byte[] ZERO
        Helper, a 0 as ZipShort.
      • LZERO

        private static final byte[] LZERO
        Helper, a 0 as ZipLong.
      • ONE

        private static final byte[] ONE
      • zipEncoding

        private ZipEncoding zipEncoding
        The zip encoding to use for file names and the file comment. This field is of internal use and will be set in setEncoding(String).
      • def

        protected final java.util.zip.Deflater def
        This Deflater object is used for output.
      • channel

        private final java.nio.channels.SeekableByteChannel channel
        Optional random access output.
      • out

        private final java.io.OutputStream out
      • useUTF8Flag

        private boolean useUTF8Flag
        whether to use the general purpose bit flag when writing UTF-8 file names or not.
      • fallbackToUTF8

        private boolean fallbackToUTF8
        Whether to encode non-encodable file names as UTF-8.
      • hasUsedZip64

        private boolean hasUsedZip64
        Whether anything inside this archive has used a ZIP64 feature.
        Since:
        1.3
      • copyBuffer

        private final byte[] copyBuffer
      • calendarInstance

        private final java.util.Calendar calendarInstance
      • LFH_SIG

        static final byte[] LFH_SIG
        local file header signature
      • DD_SIG

        static final byte[] DD_SIG
        data descriptor signature
      • CFH_SIG

        static final byte[] CFH_SIG
        central file header signature
      • EOCD_SIG

        static final byte[] EOCD_SIG
        end of central dir signature
      • ZIP64_EOCD_SIG

        static final byte[] ZIP64_EOCD_SIG
        ZIP64 end of central dir signature
      • ZIP64_EOCD_LOC_SIG

        static final byte[] ZIP64_EOCD_LOC_SIG
        ZIP64 end of central dir locator signature
    • Constructor Detail

      • ZipArchiveOutputStream

        public ZipArchiveOutputStream​(java.io.OutputStream out)
        Creates a new ZIP OutputStream filtering the underlying stream.
        Parameters:
        out - the outputstream to zip
      • ZipArchiveOutputStream

        public ZipArchiveOutputStream​(java.io.File file)
                               throws java.io.IOException
        Creates a new ZIP OutputStream writing to a File. Will use random access if possible.
        Parameters:
        file - the file to zip to
        Throws:
        java.io.IOException - on error
      • ZipArchiveOutputStream

        public ZipArchiveOutputStream​(java.nio.channels.SeekableByteChannel channel)
                               throws java.io.IOException
        Creates a new ZIP OutputStream writing to a SeekableByteChannel.

        SeekableInMemoryByteChannel allows you to write to an in-memory archive using random access.

        Parameters:
        channel - the channel to zip to
        Throws:
        java.io.IOException - on error
        Since:
        1.13
    • Method Detail

      • isSeekable

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

        For seekable streams, you don't need to calculate the CRC or uncompressed size for STORED entries before invoking putArchiveEntry(ArchiveEntry).

        Returns:
        true if seekable
      • setEncoding

        public void setEncoding​(java.lang.String encoding)
        The encoding to use for file names and the file comment.

        For a list of possible values see http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html. Defaults to UTF-8.

        Parameters:
        encoding - the encoding to use for file names, use null for the platform's default encoding
      • getEncoding

        public java.lang.String getEncoding()
        The encoding to use for file names and the file comment.
        Returns:
        null if using the platform's default character encoding.
      • setUseLanguageEncodingFlag

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

        Defaults to true.

        Parameters:
        b - whether to set the language encoding flag if the file name encoding is UTF-8
      • setCreateUnicodeExtraFields

        public void setCreateUnicodeExtraFields​(ZipArchiveOutputStream.UnicodeExtraFieldPolicy b)
        Whether to create Unicode Extra Fields.

        Defaults to NEVER.

        Parameters:
        b - whether to create Unicode Extra Fields.
      • setFallbackToUTF8

        public void setFallbackToUTF8​(boolean b)
        Whether to fall back to UTF and the language encoding flag if the file name cannot be encoded using the specified encoding.

        Defaults to false.

        Parameters:
        b - whether to fall back to UTF and the language encoding flag if the file name cannot be encoded using the specified encoding.
      • writeCentralDirectoryInChunks

        private void writeCentralDirectoryInChunks()
                                            throws java.io.IOException
        Throws:
        java.io.IOException
      • closeEntry

        private void closeEntry​(boolean actuallyNeedsZip64,
                                boolean phased)
                         throws java.io.IOException
        Throws:
        java.io.IOException
      • preClose

        private void preClose()
                       throws java.io.IOException
        Throws:
        java.io.IOException
      • addRawArchiveEntry

        public void addRawArchiveEntry​(ZipArchiveEntry entry,
                                       java.io.InputStream rawStream)
                                throws java.io.IOException
        Adds an archive entry with a raw input stream. If crc, size and compressed size are supplied on the entry, these values will be used as-is. Zip64 status is re-established based on the settings in this stream, and the supplied value is ignored. The entry is put and closed immediately.
        Parameters:
        entry - The archive entry to add
        rawStream - The raw input stream of a different entry. May be compressed/encrypted.
        Throws:
        java.io.IOException - If copying fails
      • flushDeflater

        private void flushDeflater()
                            throws java.io.IOException
        Ensures all bytes sent to the deflater are written to the stream.
        Throws:
        java.io.IOException
      • handleSizesAndCrc

        private boolean handleSizesAndCrc​(long bytesWritten,
                                          long crc,
                                          Zip64Mode effectiveMode)
                                   throws java.util.zip.ZipException
        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.
        Throws:
        java.util.zip.ZipException
      • checkIfNeedsZip64

        private boolean checkIfNeedsZip64​(Zip64Mode effectiveMode)
                                   throws java.util.zip.ZipException
        Verifies the sizes aren't too big in the Zip64Mode.Never case and returns whether the entry would require a Zip64 extra field.
        Throws:
        java.util.zip.ZipException
      • isTooLageForZip32

        private boolean isTooLageForZip32​(ZipArchiveEntry zipArchiveEntry)
      • rewriteSizesAndCrc

        private void rewriteSizesAndCrc​(boolean actuallyNeedsZip64)
                                 throws java.io.IOException
        When using random access output, write the local file header and potentiall the ZIP64 extra containing the correct CRC and compressed/uncompressed sizes.
        Throws:
        java.io.IOException
      • putArchiveEntry

        private void putArchiveEntry​(ArchiveEntry archiveEntry,
                                     boolean phased)
                              throws java.io.IOException
        Writes the headers for an archive entry to the output stream. The caller must then write the content to the stream and call closeArchiveEntry() to complete the process.
        Parameters:
        archiveEntry - The archiveEntry
        phased - If true size, compressedSize and crc required to be known up-front in the archiveEntry
        Throws:
        java.lang.ClassCastException - if entry is not an instance of ZipArchiveEntry
        Zip64RequiredException - if the entry's uncompressed or compressed size is known to exceed 4 GByte and setUseZip64(org.apache.commons.compress.archivers.zip.Zip64Mode) is Zip64Mode.Never.
        java.io.IOException
      • setDefaults

        private void setDefaults​(ZipArchiveEntry entry)
        Provides default values for compression method and last modification time.
      • validateSizeInformation

        private void validateSizeInformation​(Zip64Mode effectiveMode)
                                      throws java.util.zip.ZipException
        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.
        Throws:
        java.util.zip.ZipException
      • shouldAddZip64Extra

        private boolean shouldAddZip64Extra​(ZipArchiveEntry entry,
                                            Zip64Mode mode)
        Whether to addd a Zip64 extended information extra field to the local file header.

        Returns true if

        • mode is Always
        • or we already know it is going to be needed
        • or the size is unknown and we can ensure it won't hurt other implementations if we add it (i.e. we can erase its usage
      • setComment

        public void setComment​(java.lang.String comment)
        Set the file comment.
        Parameters:
        comment - the comment
      • setLevel

        public void setLevel​(int level)
        Sets the compression level for subsequent entries.

        Default is Deflater.DEFAULT_COMPRESSION.

        Parameters:
        level - the compression level.
        Throws:
        java.lang.IllegalArgumentException - if an invalid compression level is specified.
      • setMethod

        public void setMethod​(int method)
        Sets the default compression method for subsequent entries.

        Default is DEFLATED.

        Parameters:
        method - an int from java.util.zip.ZipEntry
      • canWriteEntryData

        public boolean canWriteEntryData​(ArchiveEntry ae)
        Whether this stream is able to write the given entry.

        May return false if it is set up to use encryption or a compression method that hasn't been implemented yet.

        Overrides:
        canWriteEntryData in class ArchiveOutputStream
        Parameters:
        ae - the entry to test
        Returns:
        This implementation always returns true.
        Since:
        1.1
      • write

        public void write​(byte[] b,
                          int offset,
                          int length)
                   throws java.io.IOException
        Writes bytes to ZIP entry.
        Overrides:
        write in class java.io.OutputStream
        Parameters:
        b - the byte array to write
        offset - the start position to write from
        length - the number of bytes to write
        Throws:
        java.io.IOException - on error
      • writeCounted

        private void writeCounted​(byte[] data)
                           throws java.io.IOException
        Write bytes to output or random access file.
        Parameters:
        data - the byte array to write
        Throws:
        java.io.IOException - on error
      • copyFromZipInputStream

        private void copyFromZipInputStream​(java.io.InputStream src)
                                     throws java.io.IOException
        Throws:
        java.io.IOException
      • close

        public void close()
                   throws java.io.IOException
        Closes this output stream and releases any system resources associated with the stream.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.io.OutputStream
        Throws:
        java.io.IOException - if an I/O error occurs.
        Zip64RequiredException - if the archive's size exceeds 4 GByte or there are more than 65535 entries inside the archive and setUseZip64(org.apache.commons.compress.archivers.zip.Zip64Mode) is Zip64Mode.Never.
      • flush

        public void flush()
                   throws java.io.IOException
        Flushes this output stream and forces any buffered output bytes to be written out to the stream.
        Specified by:
        flush in interface java.io.Flushable
        Overrides:
        flush in class java.io.OutputStream
        Throws:
        java.io.IOException - if an I/O error occurs.
      • deflate

        protected final void deflate()
                              throws java.io.IOException
        Writes next block of compressed data to the output stream.
        Throws:
        java.io.IOException - on error
      • writeLocalFileHeader

        protected void writeLocalFileHeader​(ZipArchiveEntry ze)
                                     throws java.io.IOException
        Writes the local file header entry
        Parameters:
        ze - the entry to write
        Throws:
        java.io.IOException - on error
      • writeLocalFileHeader

        private void writeLocalFileHeader​(ZipArchiveEntry ze,
                                          boolean phased)
                                   throws java.io.IOException
        Throws:
        java.io.IOException
      • createLocalFileHeader

        private byte[] createLocalFileHeader​(ZipArchiveEntry ze,
                                             java.nio.ByteBuffer name,
                                             boolean encodable,
                                             boolean phased,
                                             long archiveOffset)
      • addUnicodeExtraFields

        private void addUnicodeExtraFields​(ZipArchiveEntry ze,
                                           boolean encodable,
                                           java.nio.ByteBuffer name)
                                    throws java.io.IOException
        Adds UnicodeExtra fields for name and file comment if mode is ALWAYS or the data cannot be encoded using the configured encoding.
        Throws:
        java.io.IOException
      • writeDataDescriptor

        protected void writeDataDescriptor​(ZipArchiveEntry ze)
                                    throws java.io.IOException
        Writes the data descriptor entry.
        Parameters:
        ze - the entry to write
        Throws:
        java.io.IOException - on error
      • writeCentralFileHeader

        protected void writeCentralFileHeader​(ZipArchiveEntry ze)
                                       throws java.io.IOException
        Writes the central file header entry.
        Parameters:
        ze - the entry to write
        Throws:
        java.io.IOException - on error
        Zip64RequiredException - if the archive's size exceeds 4 GByte and #setUseZip64 is Zip64Mode.Never.
      • createCentralFileHeader

        private byte[] createCentralFileHeader​(ZipArchiveEntry ze)
                                        throws java.io.IOException
        Throws:
        java.io.IOException
      • createCentralFileHeader

        private byte[] createCentralFileHeader​(ZipArchiveEntry ze,
                                               java.nio.ByteBuffer name,
                                               ZipArchiveOutputStream.EntryMetaData entryMetaData,
                                               boolean needsZip64Extra)
                                        throws java.io.IOException
        Writes the central file header entry.
        Parameters:
        ze - the entry to write
        name - The encoded name
        entryMetaData - meta data for this file
        Throws:
        java.io.IOException - on error
      • handleZip64Extra

        private void handleZip64Extra​(ZipArchiveEntry ze,
                                      long lfhOffset,
                                      boolean needsZip64Extra)
        If the entry needs Zip64 extra information inside the central directory then configure its data.
      • writeCentralDirectoryEnd

        protected void writeCentralDirectoryEnd()
                                         throws java.io.IOException
        Writes the "End of central dir record".
        Throws:
        java.io.IOException - on error
        Zip64RequiredException - if the archive's size exceeds 4 GByte or there are more than 65535 entries inside the archive and #setUseZip64 is Zip64Mode.Never.
      • writeZip64CentralDirectory

        protected void writeZip64CentralDirectory()
                                           throws java.io.IOException
        Writes the "ZIP64 End of central dir record" and "ZIP64 End of central dir locator".
        Throws:
        java.io.IOException - on error
        Since:
        1.3
      • writeOut

        protected final void writeOut​(byte[] data)
                               throws java.io.IOException
        Write bytes to output or random access file.
        Parameters:
        data - the byte array to write
        Throws:
        java.io.IOException - on error
      • writeOut

        protected final void writeOut​(byte[] data,
                                      int offset,
                                      int length)
                               throws java.io.IOException
        Write bytes to output or random access file.
        Parameters:
        data - the byte array to write
        offset - the start position to write from
        length - the number of bytes to write
        Throws:
        java.io.IOException - on error
      • getGeneralPurposeBits

        private GeneralPurposeBit getGeneralPurposeBits​(boolean utfFallback,
                                                        boolean usesDataDescriptor)
      • versionNeededToExtract

        private int versionNeededToExtract​(int zipMethod,
                                           boolean zip64,
                                           boolean usedDataDescriptor)
      • usesDataDescriptor

        private boolean usesDataDescriptor​(int zipMethod,
                                           boolean phased)
      • versionNeededToExtractMethod

        private int versionNeededToExtractMethod​(int zipMethod)
      • createArchiveEntry

        public ArchiveEntry createArchiveEntry​(java.io.File inputFile,
                                               java.lang.String entryName)
                                        throws java.io.IOException
        Creates a new zip entry taking some information from the given file and using the provided name.

        The name will be adjusted to end with a forward slash "/" if the file is a directory. If the file is not a directory a potential trailing forward slash will be stripped from the entry name.

        Must not be used if the stream has already been closed.

        Specified by:
        createArchiveEntry in class ArchiveOutputStream
        Parameters:
        inputFile - the file to create the entry from
        entryName - name to use for the entry
        Returns:
        the ArchiveEntry set up with details from the file
        Throws:
        java.io.IOException - if an I/O error occurs
      • hasZip64Extra

        private boolean hasZip64Extra​(ZipArchiveEntry ze)
        Is there a ZIP64 extended information extra field for the entry?
        Since:
        1.3
      • getEffectiveZip64Mode

        private Zip64Mode getEffectiveZip64Mode​(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.
        Since:
        1.3
      • getName

        private java.nio.ByteBuffer getName​(ZipArchiveEntry ze)
                                     throws java.io.IOException
        Throws:
        java.io.IOException
      • destroy

        void destroy()
              throws java.io.IOException
        Closes the underlying stream/file without finishing the archive, the result will likely be a corrupt archive.

        This method only exists to support tests that generate corrupt archives so they can clean up any temporary files.

        Throws:
        java.io.IOException