Package 

Class ZipArchiveOutputStream

  • All Implemented Interfaces:
    java.io.Closeable , java.io.Flushable , java.lang.AutoCloseable

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

    • Method Summary

      Modifier and Type Method Description
      void setComment(String comment) Set the file comment.
      void setLevel(int level) Sets the compression level for subsequent entries.
      void setMethod(int method) Sets the default compression method for subsequent entries.
      String getEncoding() The encoding to use for file names and the file comment.
      void setEncoding(String encoding) The encoding to use for file names and the file comment.
      void setFallbackToUTF8(boolean b) Whether to fall back to UTF and the language encoding flag ifthe file name cannot be encoded using the specified encoding.
      void setCreateUnicodeExtraFields(ZipArchiveOutputStream.UnicodeExtraFieldPolicy b) Whether to create Unicode Extra Fields.
      void addRawArchiveEntry(ZipArchiveEntry entry, InputStream rawStream) 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 valueis ignored.The entry is put and closed immediately.
      boolean canWriteEntryData(ArchiveEntry ae) Whether this stream is able to write the given entry.
      void close() Closes this output stream and releases any system resourcesassociated with the stream.
      void closeArchiveEntry() Writes all necessary data for this entry.
      ArchiveEntry createArchiveEntry(File inputFile, String entryName) Creates a new zip entry taking some information from the givenfile and using the provided name.
      void finish() Finishes the addition of entries to this stream, without closing it.Additional data can be written, if the format supports it.
      void flush() Flushes this output stream and forces any buffered output bytesto be written out to the stream.
      long getBytesWritten() Returns the total number of bytes written to this stream.
      boolean isSeekable() This method indicates whether this archive is writing to aseekable stream (i.e., to a random access file).
      void putArchiveEntry(ArchiveEntry archiveEntry) 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.
      void setUseLanguageEncodingFlag(boolean b) Whether to set the language encoding flag if the file nameencoding is UTF-8.
      void setUseZip64(Zip64Mode mode) Whether Zip64 extensions will be used.
      void write(Array<byte> b, int offset, int length) Writes bytes to ZIP entry.
      void writePreamble(Array<byte> preamble) Write preamble data.
      void writePreamble(Array<byte> preamble, int offset, int length) Write preamble data.
      • Methods inherited from class org.readium.r2.shared.util.zip.compress.archivers.ArchiveOutputStream

        canWriteEntryData, createArchiveEntry, getCount, putArchiveEntry, write
      • Methods inherited from class java.io.OutputStream

        close, flush, nullOutputStream, write
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ZipArchiveOutputStream

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

        ZipArchiveOutputStream(File file, long zipSplitSize)
        Creates a split ZIP Archive.
        Parameters:
        file - the file that will become the last part of the split archive
        zipSplitSize - maximum size of a single part of the splitarchive created by this stream.
      • ZipArchiveOutputStream

        ZipArchiveOutputStream(SeekableByteChannel channel)
        Creates a new ZIP OutputStream writing to a SeekableByteChannel.
        Parameters:
        channel - the channel to zip to
    • Method Detail

      • setComment

         void setComment(String comment)

        Set the file comment.

        Parameters:
        comment - the comment
      • setLevel

         void setLevel(int level)

        Sets the compression level for subsequent entries.

        Default is Deflater.DEFAULT_COMPRESSION.

        Parameters:
        level - the compression level.
      • setMethod

         void setMethod(int method)

        Sets the default compression method for subsequent entries.

        Default is DEFLATED.

        Parameters:
        method - an {@code int} from java.util.zip.
      • getEncoding

         String getEncoding()

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

      • setEncoding

         void setEncoding(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 nullfor the platform's default encoding
      • setFallbackToUTF8

         void setFallbackToUTF8(boolean b)

        Whether to fall back to UTF and the language encoding flag ifthe file name cannot be encoded using the specified encoding.

        Defaults to false.

        Parameters:
        b - whether to fall back to UTF and the language encodingflag if the file name cannot be encoded using the specifiedencoding.
      • addRawArchiveEntry

         void addRawArchiveEntry(ZipArchiveEntry entry, InputStream rawStream)

        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 valueis ignored.The entry is put and closed immediately.

        Parameters:
        entry - The archive entry to add
        rawStream - The raw input stream of a different entry.
      • canWriteEntryData

         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 acompression method that hasn't been implemented yet.

      • close

         void close()

        Closes this output stream and releases any system resourcesassociated with the stream.

      • closeArchiveEntry

         void closeArchiveEntry()

        Writes all necessary data for this entry.

      • createArchiveEntry

         ArchiveEntry createArchiveEntry(File inputFile, String entryName)

        Creates a new zip entry taking some information from the givenfile and using the provided name.

        The name will be adjusted to end with a forward slash "/" ifthe file is a directory. If the file is not a directory apotential trailing forward slash will be stripped from theentry name.

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

      • finish

         void finish()

        Finishes the addition of entries to this stream, without closing it.Additional data can be written, if the format supports it.

      • flush

         void flush()

        Flushes this output stream and forces any buffered output bytesto be written out to the stream.

      • getBytesWritten

         long getBytesWritten()

        Returns the total number of bytes written to this stream.

      • isSeekable

         boolean isSeekable()

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

        For seekable streams, you don't need to calculate the CRC oruncompressed size for STORED entries beforeinvoking putArchiveEntry.

      • putArchiveEntry

         void putArchiveEntry(ArchiveEntry archiveEntry)

        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.

      • setUseLanguageEncodingFlag

         void setUseLanguageEncodingFlag(boolean b)

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

        Defaults to true.

        Parameters:
        b - whether to set the language encoding flag if the filename encoding is UTF-8
      • setUseZip64

         void setUseZip64(Zip64Mode mode)

        Whether Zip64 extensions will be used.

        When setting the mode to Never, putArchiveEntry, closeArchiveEntry, or close may throw a if the entry's size or the total sizeof the archive exceeds 4GB or there are more than 65536 entriesinside the archive. Any archive created in this mode will bereadable by implementations that don't support Zip64.

        When setting the mode to Always,Zip64 extensions will be used for all entries. Any archivecreated in this mode may be unreadable by implementations thatdon't support Zip64 even if all its contents would be.

        When setting the mode to AsNeeded, Zip64 extensions will transparently be used forthose entries that require them. This mode can only be used ifthe uncompressed size of the ZipArchiveEntry is knownwhen calling putArchiveEntry -this mode is not valid when the output stream is not seekableand the uncompressed size is unknown when is called.

        If no entry inside the resulting archive requires Zip64extensions then Never will create thesmallest archive. AsNeeded willcreate a slightly bigger archive if the uncompressed size ofany entry has initially been unknown and create an archiveidentical to Never otherwise. Always will create an archive that is atleast 24 bytes per entry bigger than the one Never would create.

        Defaults to AsNeeded unless putArchiveEntry is called with an entry of unknownsize and data is written to a non-seekable stream - in thiscase the default is Never.

        Parameters:
        mode - Whether Zip64 extensions will be used.
      • write

         void write(Array<byte> b, int offset, int length)

        Writes bytes to ZIP entry.

        Parameters:
        b - the byte array to write
        offset - the start position to write from
        length - the number of bytes to write
      • writePreamble

         void writePreamble(Array<byte> preamble)

        Write preamble data. For most of time, this is used tomake self-extracting zips.

        Parameters:
        preamble - data to write
      • writePreamble

         void writePreamble(Array<byte> preamble, int offset, int length)

        Write preamble data. For most of time, this is used tomake self-extracting zips.

        Parameters:
        preamble - data to write
        offset - the start offset in the data
        length - the number of bytes to write