Package proguard.io

Class ZipOutput

java.lang.Object
proguard.io.ZipOutput

public class ZipOutput extends 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 
    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.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
     
  • Constructor Summary

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

    Modifier and Type
    Method
    Description
    void
    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.
    createOutputStream(String name, boolean compress, int modificationTime)
    Creates a new zip entry, returning an output stream to write its data.
    createOutputStream(String name, boolean compress, int uncompressedAlignment, int modificationTime)
    Creates a new zip entry, returning an output stream to write its data.
    createOutputStream(String name, boolean compress, int uncompressedAlignment, int modificationTime, byte[] extraField, String comment)
    Creates a new zip entry, returning an output stream to write its data.
    static void
    main(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
    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
    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
    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
  • Field Details

  • Constructor Details

    • ZipOutput

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

      public ZipOutput(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(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(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(OutputStream outputStream, int uncompressedAlignment, boolean useZip64, 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(OutputStream outputStream, byte[] header, int uncompressedAlignment, boolean useZip64) throws 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:
      IOException
    • ZipOutput

      public ZipOutput(OutputStream outputStream, byte[] header, int uncompressedAlignment, boolean useZip64, String comment) throws 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:
      IOException
  • Method Details

    • createOutputStream

      public OutputStream createOutputStream(String name, boolean compress, int modificationTime) throws 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:
      IOException
    • createOutputStream

      public OutputStream createOutputStream(String name, boolean compress, int uncompressedAlignment, int modificationTime) throws 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:
      IOException
    • createOutputStream

      public OutputStream createOutputStream(String name, boolean compress, int uncompressedAlignment, int modificationTime, byte[] extraField, String comment) throws 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:
      IOException
    • close

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

      public void close(long centralDirectoryOffset) throws IOException
      Closes the zip archive, writing out its central directory and closing the underlying output stream.
      Throws:
      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 IOException
      Writes the zip entries in the central directory.
      Returns:
      the total size of the directory entries.
      Throws:
      IOException
    • writeEndOfCentralDirectory

      protected void writeEndOfCentralDirectory(long centralDirectoryOffset, long centralDirectorySize) throws 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:
      IOException
    • writeZip64EndOfCentralDirectory

      protected void writeZip64EndOfCentralDirectory(long centralDirectoryOffset, long centralDirectorySize) throws 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:
      IOException - In case a problem occurs during writing.
    • writeZip64EndOfCentralDirectoryLocator

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

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

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

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

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

      public static void main(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.