Class Compression
- All Implemented Interfaces:
AutoCloseable
Compress and decompress bytes, text, files, and streams with a flexible API.
Chilkat.Compression provides a general-purpose API for compressing
and decompressing data in memory, files, and streams. It supports multiple
algorithms, text charset conversion, encoded output, chunked processing, and
streaming operations for large data.
Multiple algorithms
Supports deflate, zlib,
bzip2, and lzw.
Flexible data types
Work with byte arrays, strings, BinData,
StringBuilder, files, and streams.
Chunked processing
Process data incrementally with FirstChunk and
LastChunk for chunk-aware methods.
Encoded output
Return compressed bytes as text using encodings such as
base64, hex, and others.
Streaming support
Compress or decompress large files and streams with stable memory usage.
Compression + encryption
Combine compression and encryption in file workflows with integrated methods.
Tip:
For text compression, explicitly set Charset = "utf-8" unless
another encoding is required. For encoded compressed data, set
EncodingMode to the desired output format.
For an extended overview, see Compression Class Overview. See also Binary Encodings for supported encoded output formats.
</section>
Event-capable: long-running methods report progress through a ProgressListener installed
with setProgressListener(ProgressListener), onAbortCheck(BooleanSupplier),
onPercentDone(IntPredicate) or onProgressInfo(BiConsumer).
Instances hold a native Chilkat object: close them with ChilkatObject.close() (try-with-resources) when
done; an unclosed instance is freed when it becomes unreachable.
- See Also:
- Online reference
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidcompressBd(BinData bd)Compresses the data contained in aBinDataobject.voidcompressBd2(BinData bdIn, BinData bdOut)Compresses the data in oneBinDataobject and appends the compressed output to anotherBinDataobject.voidcompressEncryptFile(JsonObject cryptParams, String srcPath, String destPath)Compresses and encrypts a file, writing the result to a destination file.voidcompressFile(String srcPath, String destPath)Compresses a source file and writes the compressed data to a destination file.voidcompressSb(StringBuilder sb, BinData binData)Compresses the text contained in aStringBuilderand appends the compressed bytes to aBinDataobject.voidcompressStream(Stream strm)Compresses data from a stream source and writes the compressed data to the stream sink.compressStringENC(String str)Compresses a string and returns the compressed result as an encoded string.voiddecompressBd(BinData bd)Decompresses the compressed data contained in aBinDataobject.voiddecompressBd2(BinData bdIn, BinData bdOut)Decompresses the data in oneBinDataobject and appends the decompressed output to anotherBinDataobject.voiddecompressFile(String srcPath, String destPath)Decompresses a source file and writes the decompressed data to a destination file.voiddecompressSb(BinData binData, StringBuilder sb)Decompresses compressed data from aBinDataobject and appends the resulting text to aStringBuilder.voiddecompressStream(Stream strm)Decompresses data from a stream source and writes the decompressed data to the stream sink.decompressStringENC(String encodedCompressedData)Decompresses compressed data supplied as an encoded string and returns the resulting text.voiddecryptDecompressFile(JsonObject cryptParams, String srcPath, String destPath)Decrypts and decompresses a file, writing the restored data to a destination file.Specifies the compression algorithm to use.Specifies the character encoding used when converting text to bytes before compression, and bytes back to text after decompression.intSets the compression level used by thedeflateandzlibalgorithms.Specifies the text encoding used by methods whose names end inENC, such asCompressBytesENCandDecompressStringENC.booleanIndicates that the next chunk-aware compression or decompression call is the first chunk in a sequence.intSpecifies the interval, in milliseconds, betweenAbortCheckevent callbacks.booleanIndicates that the next chunk-aware compression or decompression call is the final chunk in a sequence.Returns the installed listener, ornull.Provides a way to enable specialized or uncommon behavior.voidonAbortCheck(BooleanSupplier callback)Installs a listener whose only callback isabortCheck: called everyHeartbeatMsmilliseconds during a long-running method; returntrueto abort it.voidonPercentDone(IntPredicate callback)Installs a listener whose only callback ispercentDone: called with the completion percentage (0..100) as a long-running method progresses; returntrueto abort it.voidonProgressInfo(BiConsumer<String,String> callback)Installs a listener whose only callback isprogressInfo: called with a name/value pair of progress information as a long-running method proceeds.voidsetAlgorithm(String value)Sets theAlgorithmproperty; seegetAlgorithm().voidsetCharset(String value)Sets theCharsetproperty; seegetCharset().voidsetDeflateLevel(int value)Sets theDeflateLevelproperty; seegetDeflateLevel().voidsetEncodingMode(String value)Sets theEncodingModeproperty; seegetEncodingMode().voidsetFirstChunk(boolean value)Sets theFirstChunkproperty; seegetFirstChunk().voidsetHeartbeatMs(int value)Sets theHeartbeatMsproperty; seegetHeartbeatMs().voidsetLastChunk(boolean value)Sets theLastChunkproperty; seegetLastChunk().voidsetProgressListener(ProgressListener listener)Installs the listener that receives this object's progress events (abortCheck,percentDone,progressInfo) during its long-running methods; replaces any listener or lambda installed before;nullremoves it.voidsetUncommonOptions(String value)Sets theUncommonOptionsproperty; seegetUncommonOptions().Methods inherited from class com.chilkatsoft.chilkat2.ChilkatObject
close, getDebugLogFilePath, getLastErrorHtml, getLastErrorText, getLastErrorXml, getLastMethodSuccess, getVerboseLogging, getVersion, isClosed, setDebugLogFilePath, setLastMethodSuccess, setVerboseLogging, toString
-
Constructor Details
-
Compression
public Compression()Creates a newCompressionobject.
-
-
Method Details
-
getAlgorithm
Specifies the compression algorithm to use. Supported values aredeflate,zlib,gzip,bzip2, andlzw.The
zliboption is the deflate algorithm with a zlib header.The
gzipoption (starting in v11.7.0) is the deflate algorithm in the gzip format (RFC 1952): a gzip header, the deflate data, and a trailer with the CRC-32 and size of the uncompressed data. When decompressing in one call, the CRC-32 and size are checked. Before v11.7.0,gzipwas not recognized and was treated asdeflate.Note:
ppmdis deprecated and should not be used. It was only available on 32-bit systems and specifically used the J variant. New applications should use one of the supported algorithms listed above.- Returns:
- the value of the
Algorithmproperty
-
setAlgorithm
Sets theAlgorithmproperty; seegetAlgorithm().- Parameters:
value- the new value (notnull)
-
getCharset
Specifies the character encoding used when converting text to bytes before compression, and bytes back to text after decompression.The current default is the computer’s ANSI charset, such as Windows-1252 on many Western Windows systems. However, most modern applications should explicitly set this property to
utf-8.Recommendation: Set
Charset = "utf-8"unless you specifically need compatibility with another encoding.- Returns:
- the value of the
Charsetproperty
-
setCharset
Sets theCharsetproperty; seegetCharset().- Parameters:
value- the new value (notnull)
-
getDeflateLevel
public int getDeflateLevel()Sets the compression level used by thedeflateandzlibalgorithms.0means no compression.9means maximum compression.- The default value is
6.
Higher values may produce smaller output but can require more processing time.
- Returns:
- the value of the
DeflateLevelproperty
-
setDeflateLevel
public void setDeflateLevel(int value)Sets theDeflateLevelproperty; seegetDeflateLevel().- Parameters:
value- the new value
-
getEncodingMode
Specifies the text encoding used by methods whose names end inENC, such asCompressBytesENCandDecompressStringENC.Compression methods ending in
ENCreturn compressed binary data as an encoded string. Decompression methods ending inENCexpect the input string to use this same encoding.Valid values include:
base64hexurlquoted-printable
- Returns:
- the value of the
EncodingModeproperty
-
setEncodingMode
Sets theEncodingModeproperty; seegetEncodingMode().- Parameters:
value- the new value (notnull)
-
getFirstChunk
public boolean getFirstChunk()Indicates that the next chunk-aware compression or decompression call is the first chunk in a sequence.The default value is
.trueWhen both
FirstChunkandLastChunkare, the method call is treated as a complete, single-call compression or decompression operation.true- Returns:
- the value of the
FirstChunkproperty
-
setFirstChunk
public void setFirstChunk(boolean value)Sets theFirstChunkproperty; seegetFirstChunk().- Parameters:
value- the new value
-
getHeartbeatMs
public int getHeartbeatMs()Specifies the interval, in milliseconds, betweenAbortCheckevent callbacks.This allows an application to periodically check whether a long-running operation should be aborted.
The default value is
0, which disablesAbortCheckcallbacks.- Returns:
- the value of the
HeartbeatMsproperty
-
setHeartbeatMs
public void setHeartbeatMs(int value)Sets theHeartbeatMsproperty; seegetHeartbeatMs().- Parameters:
value- the new value
-
getLastChunk
public boolean getLastChunk()Indicates that the next chunk-aware compression or decompression call is the final chunk in a sequence.The default value is
.trueWhen both
FirstChunkandLastChunkare, the input is treated as the complete data set and processed in a single call.true- Returns:
- the value of the
LastChunkproperty
-
setLastChunk
public void setLastChunk(boolean value)Sets theLastChunkproperty; seegetLastChunk().- Parameters:
value- the new value
-
getUncommonOptions
Provides a way to enable specialized or uncommon behavior. This property normally remains empty.It may be set to a comma-separated list of keywords.
Supported option:
-
Crypt2CompressHdr— Duplicates the compression/decompression header behavior used by the deprecated and removed Crypt2 compression functions.
- Returns:
- the value of the
UncommonOptionsproperty
-
-
setUncommonOptions
Sets theUncommonOptionsproperty; seegetUncommonOptions().- Parameters:
value- the new value (notnull)
-
compressBd
Compresses the data contained in aBinDataobject.The
BinDataobject is modified to contain the compressed result.This method is not
FirstChunk/LastChunkaware.- Parameters:
bd- thebdargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
compressBd2
Compresses the data in oneBinDataobject and appends the compressed output to anotherBinDataobject.The input
BinDatais not modified.This method is
FirstChunk/LastChunkaware.- Parameters:
bdIn- thebdInargument (notnull)bdOut- thebdOutargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
compressEncryptFile
Compresses and encrypts a file, writing the result to a destination file.The compression and encryption are performed internally in streaming mode, so files of any size can be processed without loading the entire file into memory.
- Parameters:
cryptParams- thecryptParamsargument (notnull)srcPath- thesrcPathargument (notnull)destPath- thedestPathargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
compressFile
Compresses a source file and writes the compressed data to a destination file.The file is processed internally in streaming mode, allowing files of any size to be compressed with stable memory usage.
- Parameters:
srcPath- thesrcPathargument (notnull)destPath- thedestPathargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
compressSb
Compresses the text contained in aStringBuilderand appends the compressed bytes to aBinDataobject.Text is converted to bytes according to the
Charsetproperty.This method is
FirstChunk/LastChunkaware.- Parameters:
sb- thesbargument (notnull)binData- thebinDataargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
compressStream
Compresses data from a stream source and writes the compressed data to the stream sink.The operation is performed in streaming mode, making it suitable for very large or even continuous streams while maintaining stable memory usage.
- Parameters:
strm- thestrmargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
compressStringENC
Compresses a string and returns the compressed result as an encoded string.The string is first converted to bytes using
Charset, then compressed, and finally encoded according toEncodingMode.This method is not
FirstChunk/LastChunkaware.- Parameters:
str- thestrargument (notnull)- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
decompressBd
Decompresses the compressed data contained in aBinDataobject.The
BinDataobject is modified to contain the decompressed result.This method is not
FirstChunk/LastChunkaware.- Parameters:
bd- thebdargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
decompressBd2
Decompresses the data in oneBinDataobject and appends the decompressed output to anotherBinDataobject.The input
BinDatais not modified.This method is
FirstChunk/LastChunkaware.- Parameters:
bdIn- thebdInargument (notnull)bdOut- thebdOutargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
decompressFile
Decompresses a source file and writes the decompressed data to a destination file.The file is processed internally in streaming mode, allowing files of any size to be decompressed without loading the entire file into memory.
- Parameters:
srcPath- thesrcPathargument (notnull)destPath- thedestPathargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
decompressSb
Decompresses compressed data from aBinDataobject and appends the resulting text to aStringBuilder.The decompressed bytes are converted to text using the
Charsetproperty.This method is
FirstChunk/LastChunkaware.- Parameters:
binData- thebinDataargument (notnull)sb- thesbargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
decompressStream
Decompresses data from a stream source and writes the decompressed data to the stream sink.The operation is performed in streaming mode, making it suitable for very large or continuous streams while maintaining stable memory usage.
- Parameters:
strm- thestrmargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
decompressStringENC
Decompresses compressed data supplied as an encoded string and returns the resulting text.The input string is decoded according to
EncodingMode, then decompressed. The resulting bytes are converted to text usingCharset.This method is not
FirstChunk/LastChunkaware.- Parameters:
encodedCompressedData- theencodedCompressedDataargument (notnull)- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
decryptDecompressFile
Decrypts and decompresses a file, writing the restored data to a destination file.This is the reverse operation of
CompressEncryptFile.- Parameters:
cryptParams- thecryptParamsargument (notnull)srcPath- thesrcPathargument (notnull)destPath- thedestPathargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
setProgressListener
Installs the listener that receives this object's progress events (abortCheck,percentDone,progressInfo) during its long-running methods; replaces any listener or lambda installed before;nullremoves it.- Parameters:
listener- the listener, ornull- Throws:
IllegalStateException- if called from inside one of this object's own callbacks
-
getProgressListener
Returns the installed listener, ornull.- Returns:
- the listener installed with
setProgressListener(ProgressListener)or one of theon...methods
-
onAbortCheck
Installs a listener whose only callback isabortCheck: called everyHeartbeatMsmilliseconds during a long-running method; returntrueto abort it. Replaces any listener installed before.- Parameters:
callback- the callback, ornullto remove the listener
-
onPercentDone
Installs a listener whose only callback ispercentDone: called with the completion percentage (0..100) as a long-running method progresses; returntrueto abort it. Replaces any listener installed before.- Parameters:
callback- the callback, ornullto remove the listener
-
onProgressInfo
Installs a listener whose only callback isprogressInfo: called with a name/value pair of progress information as a long-running method proceeds. Replaces any listener installed before.- Parameters:
callback- the callback, ornullto remove the listener
-