Class ZipArchiveOutputStream

java.lang.Object
java.io.OutputStream
org.apache.commons.compress.archivers.ArchiveOutputStream
org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable
Direct Known Subclasses:
JarArchiveOutputStream

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 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(ArchiveEntry) 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 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.

This class is not thread-safe
  • Field Details

  • Constructor Details

    • ZipArchiveOutputStream

      Creates a new ZIP OutputStream writing to a File. Will use random access if possible.
      Parameters:
      file - the file to ZIP to
      Throws:
      IOException - on error
    • ZipArchiveOutputStream

      public ZipArchiveOutputStream(File file, long zipSplitSize) throws IOException
      Creates a split ZIP Archive.

      The files making up the archive will use Z01, Z02, ... extensions and the last part of it will be the given file.

      Even though the stream writes to a file this stream will behave as if no random access was possible. This means the sizes of stored entries need to be known before the actual entry data is written.

      Parameters:
      file - the file that will become the last part of the split archive
      zipSplitSize - maximum size of a single part of the split archive created by this stream. Must be between 64kB and about 4GB.
      Throws:
      IOException - on error
      IllegalArgumentException - if zipSplitSize is not in the required range
      Since:
      1.20
    • ZipArchiveOutputStream

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

      public ZipArchiveOutputStream(Path path, long zipSplitSize) throws IOException
      Creates a split ZIP Archive.

      The files making up the archive will use Z01, Z02, ... extensions and the last part of it will be the given file.

      Even though the stream writes to a file this stream will behave as if no random access was possible. This means the sizes of stored entries need to be known before the actual entry data is written.

      Parameters:
      path - the path to the file that will become the last part of the split archive
      zipSplitSize - maximum size of a single part of the split archive created by this stream. Must be between 64kB and about 4GB.
      Throws:
      IOException - on error
      IllegalArgumentException - if zipSplitSize is not in the required range
      Since:
      1.22
    • ZipArchiveOutputStream

      public ZipArchiveOutputStream(Path file, OpenOption... options) throws IOException
      Creates a new ZIP OutputStream writing to a Path. Will use random access if possible.
      Parameters:
      file - the file to ZIP to
      options - options specifying how the file is opened.
      Throws:
      IOException - on error
      Since:
      1.21
    • ZipArchiveOutputStream

      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
      Since:
      1.13
  • Method Details