Class ZipOutputStream

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.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 Detail

      • entries

        private java.util.Vector entries
      • crc

        private final CRC32 crc
      • 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:
        Constant Field Values
      • STORED

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

        public static final int DEFLATED
        Compression method. This method uses the Deflater.
        See Also:
        Constant Field Values
    • Constructor Detail

      • ZipOutputStream

        public ZipOutputStream​(java.io.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 Detail

      • setComment

        public void setComment​(java.lang.String comment)
        Set the zip file comment.
        Parameters:
        comment - the comment.
        Throws:
        java.lang.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:
        java.lang.IllegalArgumentException - if method is not supported.
        See Also:
        STORED, DEFLATED
      • setLevel

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

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

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

        public void putNextEntry​(ZipEntry entry)
                          throws java.io.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:
        java.io.IOException - if an I/O error occured.
        ZipException - if stream was finished.
      • closeEntry

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

        public void write​(byte[] b,
                          int off,
                          int len)
                   throws java.io.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:
        java.io.IOException - if an I/O error occured.
        ZipException - if no entry is active.
      • finish

        public void finish()
                    throws java.io.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:
        java.io.IOException - if an I/O error occured.