Java deflater zip util

Java deflater zip util

This class provides support for general purpose compression using the popular ZLIB compression library. The ZLIB compression library was initially developed as part of the PNG graphics standard and is not protected by patents. It is fully described in the specifications at the java.util.zip package description. This class deflates sequences of bytes into ZLIB compressed data format. The input byte sequence is provided in either byte array or byte buffer, via one of the setInput() methods. The output byte sequence is written to the output byte array or byte buffer passed to the deflate() methods. The following code fragment demonstrates a trivial compression and decompression of a string using Deflater and Inflater .

try < // Encode a String into bytes String inputString = "blahblahblah"; byte[] input = inputString.getBytes("UTF-8"); // Compress the bytes byte[] output = new byte[100]; Deflater compresser = new Deflater(); compresser.setInput(input); compresser.finish(); int compressedDataLength = compresser.deflate(output); compresser.end(); // Decompress the bytes Inflater decompresser = new Inflater(); decompresser.setInput(output, 0, compressedDataLength); byte[] result = new byte[100]; int resultLength = decompresser.inflate(result); decompresser.end(); // Decode the bytes into a String String outputString = new String(result, 0, resultLength, "UTF-8"); >catch (java.io.UnsupportedEncodingException ex) < // handle >catch (java.util.zip.DataFormatException ex) < // handle >

Field Summary

Compression strategy best used for data consisting mostly of small values with a somewhat random distribution.

Compression flush mode used to flush out all pending output; may degrade compression for some compression algorithms.

Constructor Summary

Method Summary

Methods declared in class java.lang.Object

Field Detail

DEFLATED

public static final int DEFLATED

NO_COMPRESSION

public static final int NO_COMPRESSION

BEST_SPEED

public static final int BEST_SPEED

BEST_COMPRESSION

public static final int BEST_COMPRESSION

DEFAULT_COMPRESSION

public static final int DEFAULT_COMPRESSION

FILTERED

public static final int FILTERED

Compression strategy best used for data consisting mostly of small values with a somewhat random distribution. Forces more Huffman coding and less string matching.

HUFFMAN_ONLY

public static final int HUFFMAN_ONLY

DEFAULT_STRATEGY

public static final int DEFAULT_STRATEGY

NO_FLUSH

public static final int NO_FLUSH

SYNC_FLUSH

public static final int SYNC_FLUSH

Compression flush mode used to flush out all pending output; may degrade compression for some compression algorithms.

FULL_FLUSH

public static final int FULL_FLUSH

Compression flush mode used to flush out all pending output and reset the deflater. Using this mode too often can seriously degrade compression.

Constructor Detail

Deflater

public Deflater​(int level, boolean nowrap)

Creates a new compressor using the specified compression level. 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.

Deflater

Creates a new compressor using the specified compression level. Compressed data will be generated in ZLIB format.

Deflater

Creates a new compressor with the default compression level. Compressed data will be generated in ZLIB format.

Method Detail

setInput

public void setInput​(byte[] input, int off, int len)

Sets input data for compression. One of the setInput() methods should be called whenever needsInput() returns true indicating that more input data is required.

Читайте также:  Best site learn html

setInput

public void setInput​(byte[] input)

Sets input data for compression. One of the setInput() methods should be called whenever needsInput() returns true indicating that more input data is required.

setInput

Sets input data for compression. One of the setInput() methods should be called whenever needsInput() returns true indicating that more input data is required. The given buffer’s position will be advanced as deflate operations are performed, up to the buffer’s limit. The input buffer may be modified (refilled) between deflate operations; doing so is equivalent to creating a new buffer and setting it with this method. Modifying the input buffer’s contents, position, or limit concurrently with an deflate operation will result in undefined behavior, which may include incorrect operation results or operation failure.

setDictionary

public void setDictionary​(byte[] dictionary, int off, int len)

Sets preset dictionary for compression. A preset dictionary is used when the history buffer can be predetermined. When the data is later uncompressed with Inflater.inflate(), Inflater.getAdler() can be called in order to get the Adler-32 value of the dictionary required for decompression.

setDictionary

public void setDictionary​(byte[] dictionary)

Sets preset dictionary for compression. A preset dictionary is used when the history buffer can be predetermined. When the data is later uncompressed with Inflater.inflate(), Inflater.getAdler() can be called in order to get the Adler-32 value of the dictionary required for decompression.

setDictionary

Sets preset dictionary for compression. A preset dictionary is used when the history buffer can be predetermined. When the data is later uncompressed with Inflater.inflate(), Inflater.getAdler() can be called in order to get the Adler-32 value of the dictionary required for decompression. The bytes in given byte buffer will be fully consumed by this method. On return, its position will equal its limit.

setStrategy

public void setStrategy​(int strategy)

Sets the compression strategy to the specified value. If the compression strategy is changed, the next invocation of deflate will compress the input available so far with the old strategy (and may be flushed); the new strategy will take effect only after that invocation.

setLevel

public void setLevel​(int level)

Sets the compression level to the specified value. If the compression level is changed, the next invocation of deflate will compress the input available so far with the old level (and may be flushed); the new level will take effect only after that invocation.

needsInput

public boolean needsInput()

Returns true if no data remains in the input buffer. This can be used to determine if one of the setInput() methods should be called in order to provide more input.

finish

finished

deflate

public int deflate​(byte[] output, int off, int len)

Compresses the input data and fills specified buffer with compressed data. Returns actual number of bytes of compressed data. A return value of 0 indicates that needsInput should be called in order to determine if more input data is required. This method uses NO_FLUSH as its compression flush mode. An invocation of this method of the form deflater.deflate(b, off, len) yields the same result as the invocation of deflater.deflate(b, off, len, Deflater.NO_FLUSH) .

Читайте также:  Back to top css

deflate

public int deflate​(byte[] output)

Compresses the input data and fills specified buffer with compressed data. Returns actual number of bytes of compressed data. A return value of 0 indicates that needsInput should be called in order to determine if more input data is required. This method uses NO_FLUSH as its compression flush mode. An invocation of this method of the form deflater.deflate(b) yields the same result as the invocation of deflater.deflate(b, 0, b.length, Deflater.NO_FLUSH) .

deflate

Compresses the input data and fills specified buffer with compressed data. Returns actual number of bytes of compressed data. A return value of 0 indicates that needsInput should be called in order to determine if more input data is required. This method uses NO_FLUSH as its compression flush mode. An invocation of this method of the form deflater.deflate(output) yields the same result as the invocation of deflater.deflate(output, Deflater.NO_FLUSH) .

deflate

public int deflate​(byte[] output, int off, int len, int flush)
  • NO_FLUSH : allows the deflater to decide how much data to accumulate, before producing output, in order to achieve the best compression (should be used in normal use scenario). A return value of 0 in this flush mode indicates that needsInput() should be called in order to determine if more input data is required.
  • SYNC_FLUSH : all pending output in the deflater is flushed, to the specified output buffer, so that an inflater that works on compressed data can get all input data available so far (In particular the needsInput() returns true after this invocation if enough output space is provided). Flushing with SYNC_FLUSH may degrade compression for some compression algorithms and so it should be used only when necessary.
  • FULL_FLUSH : all pending output is flushed out as with SYNC_FLUSH . The compression state is reset so that the inflater that works on the compressed output data can restart from this point if previous compressed data has been damaged or if random access is desired. Using FULL_FLUSH too often can seriously degrade compression.

In the case of FULL_FLUSH or SYNC_FLUSH , if the return value is len , the space available in output buffer b , this method should be invoked again with the same flush parameter and more output space. Make sure that len is greater than 6 to avoid flush marker (5 bytes) being repeatedly output to the output buffer every time this method is invoked.

If the setInput(ByteBuffer) method was called to provide a buffer for input, the input buffer’s position will be advanced by the number of bytes consumed by this operation.

deflate

  • NO_FLUSH : allows the deflater to decide how much data to accumulate, before producing output, in order to achieve the best compression (should be used in normal use scenario). A return value of 0 in this flush mode indicates that needsInput() should be called in order to determine if more input data is required.
  • SYNC_FLUSH : all pending output in the deflater is flushed, to the specified output buffer, so that an inflater that works on compressed data can get all input data available so far (In particular the needsInput() returns true after this invocation if enough output space is provided). Flushing with SYNC_FLUSH may degrade compression for some compression algorithms and so it should be used only when necessary.
  • FULL_FLUSH : all pending output is flushed out as with SYNC_FLUSH . The compression state is reset so that the inflater that works on the compressed output data can restart from this point if previous compressed data has been damaged or if random access is desired. Using FULL_FLUSH too often can seriously degrade compression.
Читайте также:  Python new file object

In the case of FULL_FLUSH or SYNC_FLUSH , if the return value is equal to the remaining space of the buffer, this method should be invoked again with the same flush parameter and more output space. Make sure that the buffer has at least 6 bytes of remaining space to avoid the flush marker (5 bytes) being repeatedly output to the output buffer every time this method is invoked.

On success, the position of the given output byte buffer will be advanced by as many bytes as were produced by the operation, which is equal to the number returned by this method.

If the setInput(ByteBuffer) method was called to provide a buffer for input, the input buffer’s position will be advanced by the number of bytes consumed by this operation.

getAdler

getTotalIn

Returns the total number of uncompressed bytes input so far. Since the number of bytes may be greater than Integer.MAX_VALUE, the getBytesRead() method is now the preferred means of obtaining this information.

getBytesRead

getTotalOut

Returns the total number of compressed bytes output so far. Since the number of bytes may be greater than Integer.MAX_VALUE, the getBytesWritten() method is now the preferred means of obtaining this information.

getBytesWritten

public long getBytesWritten()

reset

Resets deflater so that a new set of input data can be processed. Keeps current compression level and strategy settings.

end

Closes the compressor and discards any unprocessed input. This method should be called when the compressor is no longer being used. Once this method is called, the behavior of the Deflater object is undefined.

finalize

@Deprecated(since="9", forRemoval=true) protected void finalize()

The finalize method has been deprecated and will be removed. It is implemented as a no-op. Subclasses that override finalize in order to perform cleanup should be modified to use alternative cleanup mechanisms and to remove the overriding finalize method. The recommended cleanup for compressor is to explicitly call end method when it is no longer in use. If the end is not invoked explicitly the resource of the compressor will be released when the instance becomes unreachable.

Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.

Источник

Оцените статью