Package proguard.io

Class ZipOutput


  • public class ZipOutput
    extends java.lang.Object
    This class writes zip data to a given output stream. It returns a new output stream for each zip entry that is opened. An entry can be compressed or uncompressed. Uncompressed entries can be aligned to a multiple of a given number of bytes.

    Multiple entries and output streams can be open at the same time. The entries are added to the central directory in the order in which they are opened, but the corresponding data are only written when their output streams are closed.

    The code automatically computes the CRC and lengths of the data, for compressed and uncompressed data.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  ZipOutput.LargeDataOutputStream
      This output stream is mostly identical to DataOutputStream, except it stores the amount of bytes written so far in a long instead of an int.
    • Constructor Summary

      Constructors 
      Constructor Description
      ZipOutput​(java.io.OutputStream outputStream)
      Creates a new ZipOutput.
      ZipOutput​(java.io.OutputStream outputStream, boolean useZip64)
      Creates a new ZipOutput.
      ZipOutput​(java.io.OutputStream outputStream, boolean useZip64, int uncompressedAlignment)
      Creates a new ZipOutput that aligns uncompressed entries.
      ZipOutput​(java.io.OutputStream outputStream, byte[] header, int uncompressedAlignment, boolean useZip64)
      Creates a new ZipOutput that aligns uncompressed entries.
      ZipOutput​(java.io.OutputStream outputStream, byte[] header, int uncompressedAlignment, boolean useZip64, java.lang.String comment)
      Creates a new ZipOutput that aligns uncompressed entries and contains a comment.
      ZipOutput​(java.io.OutputStream outputStream, int uncompressedAlignment)
      Creates a new ZipOutput that aligns uncompressed entries.
      ZipOutput​(java.io.OutputStream outputStream, int uncompressedAlignment, boolean useZip64, java.lang.String comment)
      Creates a new ZipOutput that aligns uncompressed entries and contains a comment.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Closes the zip archive, writing out its central directory and closing the underlying output stream.
      void close​(long centralDirectoryOffset)
      Closes the zip archive, writing out its central directory and closing the underlying output stream.
      java.io.OutputStream createOutputStream​(java.lang.String name, boolean compress, int modificationTime)
      Creates a new zip entry, returning an output stream to write its data.
      java.io.OutputStream createOutputStream​(java.lang.String name, boolean compress, int uncompressedAlignment, int modificationTime)
      Creates a new zip entry, returning an output stream to write its data.
      java.io.OutputStream createOutputStream​(java.lang.String name, boolean compress, int uncompressedAlignment, int modificationTime, byte[] extraField, java.lang.String comment)
      Creates a new zip entry, returning an output stream to write its data.
      static void main​(java.lang.String[] args)
      Provides a simple test for this class, creating a zip file with the given name and a few aligned/compressed/uncompressed zip entries.
      protected long size()
      Returns the current size of the data written to the output stream.
      protected void writeEndOfCentralDirectory​(long centralDirectoryOffset, long centralDirectorySize)
      Ends the central directory.
      protected long writeEntriesOfCentralDirectory()
      Writes the zip entries in the central directory.
      protected void writeInt​(int value)
      Writes out a little-endian int value to the zip output stream.
      protected void writeInt​(long value)
      Writes out a little-endian int value to the zip output stream.
      protected void writeLong​(long value)
      Writes out a little-endian long value to the zip output stream.
      protected void writeShort​(int value)
      Writes out a little-endian short value to the zip output stream.
      protected long writeStartOfCentralDirectory()
      Starts the central directory.
      protected void writeZip64EndOfCentralDirectory​(long centralDirectoryOffset, long centralDirectorySize)
      Writes out a ZIP64 end of central directory entry.
      protected void writeZip64EndOfCentralDirectoryLocator​(long zip64CentralDirectoryOffset)
      Writes out a ZIP64 end of central directory locator.
      • Methods inherited from class java.lang.Object

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

      • ZipOutput

        public ZipOutput​(java.io.OutputStream outputStream)
        Creates a new ZipOutput.
        Parameters:
        outputStream - the output stream to which the zip data will be written.
      • ZipOutput

        public ZipOutput​(java.io.OutputStream outputStream,
                         boolean useZip64)
        Creates a new ZipOutput.
        Parameters:
        outputStream - the output stream to which the zip data will be written.
        useZip64 - Whether to write out the archive in zip64 format.
      • ZipOutput

        public ZipOutput​(java.io.OutputStream outputStream,
                         int uncompressedAlignment)
        Creates a new ZipOutput that aligns uncompressed entries.
        Parameters:
        outputStream - the output stream to which the zip data will be written.
        uncompressedAlignment - the default alignment of uncompressed data.
      • ZipOutput

        public ZipOutput​(java.io.OutputStream outputStream,
                         boolean useZip64,
                         int uncompressedAlignment)
        Creates a new ZipOutput that aligns uncompressed entries.
        Parameters:
        outputStream - the output stream to which the zip data will be written.
        useZip64 - Whether to write out the archive in zip64 format.
        uncompressedAlignment - the default alignment of uncompressed data.
      • ZipOutput

        public ZipOutput​(java.io.OutputStream outputStream,
                         int uncompressedAlignment,
                         boolean useZip64,
                         java.lang.String comment)
        Creates a new ZipOutput that aligns uncompressed entries and contains a comment.
        Parameters:
        outputStream - the output stream to which the zip data will be written.
        uncompressedAlignment - the default alignment of uncompressed data.
        useZip64 - Whether to write out the archive in zip64 format.
        comment - optional comment for the entire zip file.
      • ZipOutput

        public ZipOutput​(java.io.OutputStream outputStream,
                         byte[] header,
                         int uncompressedAlignment,
                         boolean useZip64)
                  throws java.io.IOException
        Creates a new ZipOutput that aligns uncompressed entries.
        Parameters:
        outputStream - the output stream to which the zip data will be written.
        header - an optional header for the zip file.
        useZip64 - Whether to write out the archive in zip64 format.
        uncompressedAlignment - the requested alignment of uncompressed data.
        Throws:
        java.io.IOException
      • ZipOutput

        public ZipOutput​(java.io.OutputStream outputStream,
                         byte[] header,
                         int uncompressedAlignment,
                         boolean useZip64,
                         java.lang.String comment)
                  throws java.io.IOException
        Creates a new ZipOutput that aligns uncompressed entries and contains a comment.
        Parameters:
        outputStream - the output stream to which the zip data will be written.
        header - an optional header for the zip file.
        uncompressedAlignment - the requested alignment of uncompressed data.
        useZip64 - Whether to write out the archive in zip64 format.
        comment - optional comment for the entire zip file.
        Throws:
        java.io.IOException
    • Method Detail

      • createOutputStream

        public java.io.OutputStream createOutputStream​(java.lang.String name,
                                                       boolean compress,
                                                       int modificationTime)
                                                throws java.io.IOException
        Creates a new zip entry, returning an output stream to write its data. It is the caller's responsibility to close the output stream.
        Parameters:
        name - the name of the zip entry.
        compress - specifies whether the entry should be compressed with the default alignment.
        modificationTime - the modification date and time of the zip entry, in DOS format.
        Returns:
        an output stream for writing the data of the zip entry.
        Throws:
        java.io.IOException
      • createOutputStream

        public java.io.OutputStream createOutputStream​(java.lang.String name,
                                                       boolean compress,
                                                       int uncompressedAlignment,
                                                       int modificationTime)
                                                throws java.io.IOException
        Creates a new zip entry, returning an output stream to write its data. It is the caller's responsibility to close the output stream.
        Parameters:
        name - the name of the zip entry.
        compress - specifies whether the entry should be compressed.
        uncompressedAlignment - the requested alignment of uncompressed data.
        modificationTime - the modification date and time of the zip entry, in DOS format.
        Returns:
        an output stream for writing the data of the zip entry.
        Throws:
        java.io.IOException
      • createOutputStream

        public java.io.OutputStream createOutputStream​(java.lang.String name,
                                                       boolean compress,
                                                       int uncompressedAlignment,
                                                       int modificationTime,
                                                       byte[] extraField,
                                                       java.lang.String comment)
                                                throws java.io.IOException
        Creates a new zip entry, returning an output stream to write its data. It is the caller's responsibility to close the output stream.
        Parameters:
        name - the name of the zip entry.
        compress - specifies whether the entry should be compressed.
        uncompressedAlignment - the requested alignment of uncompressed data.
        modificationTime - the modification date and time of the zip entry, in DOS format.
        extraField - optional extra field data. These should contain chunks, each with a short ID, a short length (little endian), and their corresponding data. The IDs 0-31 are reserved for Pkware. Java's jar tool just specifies an ID 0xcafe on its first entry.
        comment - an optional comment.
        Returns:
        an output stream for writing the data of the zip entry.
        Throws:
        java.io.IOException
      • close

        public void close()
                   throws java.io.IOException
        Closes the zip archive, writing out its central directory and closing the underlying output stream.
        Throws:
        java.io.IOException
      • close

        public void close​(long centralDirectoryOffset)
                   throws java.io.IOException
        Closes the zip archive, writing out its central directory and closing the underlying output stream.
        Throws:
        java.io.IOException
      • size

        protected long size()
        Returns the current size of the data written to the output stream.
      • writeStartOfCentralDirectory

        protected long writeStartOfCentralDirectory()
        Starts the central directory.
        Returns:
        the current position in the output stream.
      • writeEntriesOfCentralDirectory

        protected long writeEntriesOfCentralDirectory()
                                               throws java.io.IOException
        Writes the zip entries in the central directory.
        Returns:
        the total size of the directory entries.
        Throws:
        java.io.IOException
      • writeEndOfCentralDirectory

        protected void writeEndOfCentralDirectory​(long centralDirectoryOffset,
                                                  long centralDirectorySize)
                                           throws java.io.IOException
        Ends the central directory.
        Parameters:
        centralDirectoryOffset - the offset of the central directory.
        centralDirectorySize - the size of the central directory, not counting the empty header and the trailer.
        Throws:
        java.io.IOException
      • writeZip64EndOfCentralDirectory

        protected void writeZip64EndOfCentralDirectory​(long centralDirectoryOffset,
                                                       long centralDirectorySize)
                                                throws java.io.IOException
        Writes out a ZIP64 end of central directory entry.
        Parameters:
        centralDirectoryOffset - The offset from the start of the archive to the first central directory record.
        centralDirectorySize - The total size of all central directory records.
        Throws:
        java.io.IOException - In case a problem occurs during writing.
      • writeZip64EndOfCentralDirectoryLocator

        protected void writeZip64EndOfCentralDirectoryLocator​(long zip64CentralDirectoryOffset)
                                                       throws java.io.IOException
        Writes out a ZIP64 end of central directory locator.
        Parameters:
        zip64CentralDirectoryOffset - The offset to the zip64 end of central directory record.
        Throws:
        java.io.IOException - In case a problem occurs during writing.
      • writeShort

        protected void writeShort​(int value)
                           throws java.io.IOException
        Writes out a little-endian short value to the zip output stream.
        Throws:
        java.io.IOException
      • writeInt

        protected void writeInt​(int value)
                         throws java.io.IOException
        Writes out a little-endian int value to the zip output stream.
        Throws:
        java.io.IOException
      • writeInt

        protected void writeInt​(long value)
                         throws java.io.IOException
        Writes out a little-endian int value to the zip output stream.
        Throws:
        java.io.IOException
      • writeLong

        protected void writeLong​(long value)
                          throws java.io.IOException
        Writes out a little-endian long value to the zip output stream.
        Throws:
        java.io.IOException
      • main

        public static void main​(java.lang.String[] args)
        Provides a simple test for this class, creating a zip file with the given name and a few aligned/compressed/uncompressed zip entries.