Class ZLib


  • public final class ZLib
    extends java.lang.Object
    This class implements stream compression with the ZLib library.
    zlib is designed to be a free, general-purpose, legally unencumbered - that is, not covered by any patents -- lossless data-compression library for use on virtually any computer hardware and operating system. The zlib data format is itself portable across platforms.

    http://www.zlib.net/

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int BEST_COMPRESSION
      Compression level for best compression, which is 9 in a scale from 0 (no compression) to 9 (best compression).
      static int BEST_SPEED
      Compression level for fastest compression, which is 1 in a scale from 0 (no compression) to 9 (best compression).
      static int DEFAULT_COMPRESSION
      Default compression level.
      static int DEFAULT_STRATEGY
      Default compression strategy.
      static int DEFLATED
      Compression method for the deflate algorithm (the only one currently supported).
      static int FILTERED
      Compression strategy best used for data consisting mostly of small values with a somewhat random distribution.
      static int HUFFMAN_ONLY
      Compression strategy for Huffman coding only.
      static int NO_COMPRESSION
      Compression level for no compression, which is 0 in a scale from 0 (no compression) to 9 (best compression)
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static int deflate​(int compressionLevel, Stream in, Stream out)
      Deprecated.
      use #deflate(Stream, Stream, int) instead
      static int deflate​(Stream in, Stream out)
      Deflates the given stream 'in' with the specified compression level, writing the result to the given stream 'out'.
      static int deflate​(Stream in, Stream out, int compressionLevel)
      Deflates the given stream 'in' with the specified compression level, writing the result to the given stream 'out'.
      static int deflate​(Stream in, Stream out, int compressionLevel, int strategy, boolean noWrap)
      Deflates the given stream 'in' using the specified strategy and compression level, writing the result to the given stream 'out'.
      static int inflate​(Stream in, Stream out)
      Attempts to fully read the given stream 'in', inflating and writing to the given stream 'out'.
      static int inflate​(Stream in, Stream out, int sizeIn)
      Attempts to read the number of bytes specified by 'sizeIn' from the the given stream 'in', inflating and writing to the given stream 'out'.
      static int inflate​(Stream in, Stream out, int sizeIn, boolean noWrap)
      Attempts to read the number of bytes specified by 'sizeIn' from the the given stream 'in', inflating and writing to the given stream 'out'.
      • Methods inherited from class java.lang.Object

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

      • NO_COMPRESSION

        public static final int NO_COMPRESSION
        Compression level for no compression, which is 0 in a scale from 0 (no compression) to 9 (best compression)
        See Also:
        Constant Field Values
      • BEST_SPEED

        public static final int BEST_SPEED
        Compression level for fastest compression, which is 1 in a scale from 0 (no compression) to 9 (best compression).
        See Also:
        Constant Field Values
      • BEST_COMPRESSION

        public static final int BEST_COMPRESSION
        Compression level for best compression, which is 9 in a scale from 0 (no compression) to 9 (best compression).
        See Also:
        Constant Field Values
      • DEFAULT_COMPRESSION

        public static final int DEFAULT_COMPRESSION
        Default compression level.
        See Also:
        Constant Field Values
      • DEFAULT_STRATEGY

        public static final int DEFAULT_STRATEGY
        Default compression strategy.
        See Also:
        Constant Field Values
      • DEFLATED

        public static final int DEFLATED
        Compression method for the deflate algorithm (the only one currently supported).
        See Also:
        Constant Field Values
      • FILTERED

        public static final int FILTERED
        Compression strategy best used for data consisting mostly of small values with a somewhat random distribution.
        See Also:
        Constant Field Values
      • HUFFMAN_ONLY

        public static final int HUFFMAN_ONLY
        Compression strategy for Huffman coding only.
        See Also:
        Constant Field Values
    • Method Detail

      • deflate

        public static int deflate​(Stream in,
                                  Stream out)
                           throws IOException
        Deflates the given stream 'in' with the specified compression level, writing the result to the given stream 'out'. Compressed data will be generated in ZLIB format using the default strategy and the default compression level.
        Parameters:
        in - Stream to be deflated
        out - Deflated stream.
        Returns:
        Size of the deflated stream
        Throws:
        IOException
        See Also:
        DEFAULT_COMPRESSION
      • deflate

        public static int deflate​(Stream in,
                                  Stream out,
                                  int compressionLevel)
                           throws IOException
        Deflates the given stream 'in' with the specified compression level, writing the result to the given stream 'out'. Compressed data will be generated in ZLIB format using the default strategy.
        Parameters:
        in - Stream to be deflated
        out - Deflated stream.
        compressionLevel - Desired compression level, which must be between 0 and 9, or -1 for the default compression level
        Returns:
        Size of the deflated stream
        Throws:
        IOException
        See Also:
        NO_COMPRESSION, BEST_SPEED, BEST_COMPRESSION
      • deflate

        @Deprecated
        public static int deflate​(int compressionLevel,
                                  Stream in,
                                  Stream out)
                           throws IOException
        Deprecated.
        use #deflate(Stream, Stream, int) instead
        Deflates the given stream 'in' with the specified compression level, writing the result to the given stream 'out'. Compressed data will be generated in ZLIB format using the default strategy.
        Parameters:
        compressionLevel - Desired compression level, which must be between 0 and 9, or -1 for the default compression level
        in - Stream to be deflated
        out - Deflated stream.
        Returns:
        Size of the deflated stream
        Throws:
        IOException
        See Also:
        NO_COMPRESSION, BEST_SPEED, BEST_COMPRESSION
      • deflate

        public static int deflate​(Stream in,
                                  Stream out,
                                  int compressionLevel,
                                  int strategy,
                                  boolean noWrap)
                           throws IOException
        Deflates the given stream 'in' using the specified strategy and compression level, writing the result to the given stream 'out'. If 'nowrap' is true then the ZLIB header and checksum fields will not be used in order to support the compression format used in both GZIP and PKZIP.
        Parameters:
        in - Stream to be deflated
        out - Deflated stream.
        compressionLevel - compression level, which must be between 0 and 9, or -1 for the default compression level
        strategy - the compression strategy
        noWrap - if true then use GZIP compatible compression
        Returns:
        Size of the deflated stream
        Throws:
        IOException
        See Also:
        NO_COMPRESSION, BEST_SPEED, BEST_COMPRESSION
      • inflate

        public static int inflate​(Stream in,
                                  Stream out)
                           throws IOException,
                                  ZipException
        Attempts to fully read the given stream 'in', inflating and writing to the given stream 'out'.
        Parameters:
        in - Deflated input stream
        out - Inflated output stream
        Returns:
        Size of the inflated stream
        Throws:
        IOException
        ZipException
      • inflate

        public static int inflate​(Stream in,
                                  Stream out,
                                  int sizeIn)
                           throws IOException,
                                  ZipException
        Attempts to read the number of bytes specified by 'sizeIn' from the the given stream 'in', inflating and writing to the given stream 'out'. If 'sizeIn' is -1, it will attempt to fully read the stream.
        Parameters:
        in - Deflated input stream
        out - Inflated output stream
        sizeIn - How many bytes to read, or -1 to read until in's end
        Returns:
        Size of the inflated stream
        Throws:
        ZipException
        IOException
      • inflate

        public static int inflate​(Stream in,
                                  Stream out,
                                  int sizeIn,
                                  boolean noWrap)
                           throws IOException,
                                  ZipException
        Attempts to read the number of bytes specified by 'sizeIn' from the the given stream 'in', inflating and writing to the given stream 'out'. If 'sizeIn' is -1, it will attempt to fully read the stream. If the parameter 'noWrap' is true then the ZLIB header and checksum fields will not be used. This provides compatibility with the compression format used by both GZIP and PKZIP.
        Note: When using the 'noWrap' option it is also necessary to provide an extra "dummy" byte as input. This is required by the ZLIB native library in order to support certain optimizations.
        Parameters:
        in - Deflated input stream
        out - Inflated output stream
        sizeIn - How many bytes to read, or -1 to read until in's end
        noWrap - if true then support GZIP compatible compression
        Returns:
        Size of the inflated stream
        Throws:
        ZipException
        IOException