Class ZipOutputStream

All Implemented Interfaces:
Closeable, Flushable, AutoCloseable, ZipConstants

public class ZipOutputStream extends DeflaterOutputStream implements ZipConstants
This is a FilterOutputStream that writes the files into a zip archive one after another. It has a special method to start a new zip entry. The zip entries contains information about the file name size, compressed size, CRC, etc. It includes support for STORED and DEFLATED entries. This class is not thread safe.
  • Field Details

    • entries

      private Vector entries
    • crc

      private final CRC32 crc
    • curEntry

      private ZipEntry curEntry
    • curMethod

      private int curMethod
    • size

      private int size
    • offset

      private int offset
    • zipComment

      private byte[] zipComment
    • defaultMethod

      private int defaultMethod
    • ZIP_STORED_VERSION

      private static final int ZIP_STORED_VERSION
      Our Zip version is hard coded to 1.0 resp. 2.0
      See Also:
    • ZIP_DEFLATED_VERSION

      private static final int ZIP_DEFLATED_VERSION
      See Also:
    • STORED

      public static final int STORED
      Compression method. This method doesn't compress at all.
      See Also:
    • DEFLATED

      public static final int DEFLATED
      Compression method. This method uses the Deflater.
      See Also:
  • Constructor Details

    • ZipOutputStream

      public ZipOutputStream(OutputStream out)
      Creates a new Zip output stream, writing a zip archive.
      Parameters:
      out - the output stream to which the zip archive is written.
  • Method Details

    • setComment

      public void setComment(String comment)
      Set the zip file comment.
      Parameters:
      comment - the comment.
      Throws:
      IllegalArgumentException - if encoding of comment is longer than 0xffff bytes.
    • setMethod

      public void setMethod(int method)
      Sets default compression method. If the Zip entry specifies another method its method takes precedence.
      Parameters:
      method - the method.
      Throws:
      IllegalArgumentException - if method is not supported.
      See Also:
    • setLevel

      public void setLevel(int level)
      Sets default compression level. The new level will be activated immediately.
      Throws:
      IllegalArgumentException - if level is not supported.
      See Also:
    • writeLeShort

      private final void writeLeShort(int value) throws IOException
      Write an unsigned short in little endian byte order.
      Throws:
      IOException
    • writeLeInt

      private final void writeLeInt(int value) throws IOException
      Write an int in little endian byte order.
      Throws:
      IOException
    • putNextEntry

      public void putNextEntry(ZipEntry entry) throws IOException
      Starts a new Zip entry. It automatically closes the previous entry if present. If the compression method is stored, the entry must have a valid size and crc, otherwise all elements (except name) are optional, but must be correct if present. If the time is not set in the entry, the current time is used.
      Parameters:
      entry - the entry.
      Throws:
      IOException - if an I/O error occured.
      ZipException - if stream was finished.
    • closeEntry

      public void closeEntry() throws IOException
      Closes the current entry.
      Throws:
      IOException - if an I/O error occured.
      ZipException - if no entry is active.
    • write

      public void write(byte[] b, int off, int len) throws IOException
      Writes the given buffer to the current entry.
      Overrides:
      write in class DeflaterOutputStream
      Parameters:
      b - the byte array.
      off - the offset into the byte array where to start.
      len - the number of bytes to write.
      Throws:
      IOException - if an I/O error occured.
      ZipException - if no entry is active.
    • finish

      public void finish() throws IOException
      Finishes the stream. This will write the central directory at the end of the zip file and flush the stream.
      Overrides:
      finish in class DeflaterOutputStream
      Throws:
      IOException - if an I/O error occured.