Class Gzip
- All Implemented Interfaces:
AutoCloseable
Compress, decompress, inspect, and convert GZIP data in files or memory.
Chilkat.Gzip provides GZIP compression and decompression for
file-based and in-memory workflows. It can compress and uncompress files,
strings, byte arrays, BinData, and encoded data such as Base64
or hex. It also supports .tar.gz extraction, gzip metadata such
as original filename, timestamp, and comments, and inspection of GZIP header
information without fully expanding the compressed data.
File compression
Compress files to .gz, uncompress .gz files,
and write decompressed output directly to disk.
Memory-based data
Work with byte arrays and Chilkat.BinData for applications
that need compression or decompression without temporary files.
String workflows
Compress and uncompress strings with explicit charset handling for text data that must round-trip correctly.
Encoded GZIP data
Handle GZIP data represented as Base64, hex, or other supported encoded forms when binary data must be carried as text.
GZIP metadata
Set or inspect metadata such as original filename, modified time, comments, and extra data stored in the GZIP header.
Tar-gzip and XFDL
Extract .tar.gz archives and decode XFDL content that uses
gzip-related encodings.
Common pattern:
Use Gzip when the data is a single GZIP stream, a
.gz file, or a .tar.gz archive. For ZIP archives,
use Chilkat.Zip; for raw deflate or zlib-style compression, use
Chilkat.Compression.
For an extended overview, see Gzip Class Overview.
</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 binDat)Compresses the contents of aBinDataobject in place, replacing the original data with Gzip-compressed data.voidcompressFile(String srcPath, String destPath)Compresses a file and writes the result as a Gzip file, typically with a.gzextension.voidcompressFile2(String srcPath, String embeddedFilename, String destPath)Compresses a file and writes the result as a Gzip file, while allowing a different filename to be embedded inside the Gzip data.voidcompressFileBd(String filePath, BinData bd)Compresses a file and stores the resulting Gzip data in aBinDataobject.voidcompressSb(StringBuilder sb, String charset, BinData bd)Compresses the text contained in aStringBuilderand writes the Gzip-compressed result to aBinDataobject.compressStringENC(String inStr, String charset, String encoding)Compresses a string and returns the Gzip-compressed data as an encoded string.voidcompressStringToFile(String inStr, String destCharset, String destPath)Compresses a string and writes the resulting Gzip data to a file.booleanSet this property toto request that the currently running operation be aborted.trueAn optional comment to embed in the Gzip file when aCompress*method is called.intControls the compression level used when creating Gzip data.The filename to embed in the Gzip file when aCompress*method is called.voidgetGzipInfo(String filePath, JsonObject json)Retrieves metadata stored in a Gzip file and returns the information in aJsonObject.voidgetGzipInfoBd(BinData bd, JsonObject json)Retrieves metadata stored in Gzip data contained within aBinDataobject and returns the information in aJsonObject.intSpecifies the interval, in milliseconds, betweenAbortCheckevent callbacks.Specifies the last-modified date/time to embed in the Gzip file when aCompress*method is called.Returns the installed listener, ornull.booleanControls the last-modified date/time assigned to files created byUncompress*methods.booleanChecks whether the data contained in aBinDataobject is in Gzip format.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.voidsetAbortCurrent(boolean value)Sets theAbortCurrentproperty; seegetAbortCurrent().voidsetComment(String value)Sets theCommentproperty; seegetComment().voidsetCompressionLevel(int value)Sets theCompressionLevelproperty; seegetCompressionLevel().voidSets the last-modified date/time to embed in the Gzip file when aCompress*method is called.voidsetExtraData(String encodedData, String encoding)Sets optional extra binary data to include in the Gzip header when aCompress*method is called.voidsetFilename(String value)Sets theFilenameproperty; seegetFilename().voidsetHeartbeatMs(int value)Sets theHeartbeatMsproperty; seegetHeartbeatMs().voidsetLastModStr(String value)Sets theLastModStrproperty; seegetLastModStr().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.voidsetUseCurrentDate(boolean value)Sets theUseCurrentDateproperty; seegetUseCurrentDate().voiduncompressBd(BinData binDat)Decompresses Gzip data contained in aBinDataobject in place, replacing the compressed data with the uncompressed data.voiduncompressBdToFile(BinData bd, String filePath)Decompresses Gzip data stored in aBinDataobject and writes the result to a file.voiduncompressFile(String srcPath, String destPath)Decompresses a Gzip file and writes the result to the specified output path.uncompressFileToString(String srcPath, String charset)Decompresses a Gzip file that contains text and returns the uncompressed text as a string.uncompressStringENC(String inStr, String charset, String encoding)Decompresses Gzip data provided as an encoded string and returns the uncompressed result as text.voidExtracts a.tar.gzarchive to a directory.Converts encoded and compressed XFDL data to a decompressed XML string.Methods inherited from class com.chilkatsoft.chilkat2.ChilkatObject
close, getDebugLogFilePath, getLastErrorHtml, getLastErrorText, getLastErrorXml, getLastMethodSuccess, getVerboseLogging, getVersion, isClosed, setDebugLogFilePath, setLastMethodSuccess, setVerboseLogging, toString
-
Constructor Details
-
Gzip
public Gzip()Creates a newGzipobject.
-
-
Method Details
-
getAbortCurrent
public boolean getAbortCurrent()Set this property toto request that the currently running operation be aborted. This is useful for long-running operations such as large file compression or decompression. Methods that complete quickly are generally not affected.trueIf no method is currently running, the property is automatically reset to
when the next method begins. After an abort occurs, it is also reset tofalse. Both synchronous and asynchronous operations can be aborted. For synchronous calls, another thread must set this property.false- Returns:
- the value of the
AbortCurrentproperty
-
setAbortCurrent
public void setAbortCurrent(boolean value)Sets theAbortCurrentproperty; seegetAbortCurrent().- Parameters:
value- the new value
-
getComment
An optional comment to embed in the Gzip file when aCompress*method is called.- Returns:
- the value of the
Commentproperty
-
setComment
Sets theCommentproperty; seegetComment().- Parameters:
value- the new value (notnull)
-
getCompressionLevel
public int getCompressionLevel()Controls the compression level used when creating Gzip data. The value can range from0to9.0= no compression9= maximum compression
The default value is
6, which is a typical balance between compression size and speed. Higher levels may take significantly more CPU time while producing only slightly smaller output, depending on the data.- Returns:
- the value of the
CompressionLevelproperty
-
setCompressionLevel
public void setCompressionLevel(int value)Sets theCompressionLevelproperty; seegetCompressionLevel().- Parameters:
value- the new value
-
getFilename
The filename to embed in the Gzip file when aCompress*method is called. Some Gzip extraction tools use this embedded filename as the default output filename.- Returns:
- the value of the
Filenameproperty
-
setFilename
Sets theFilenameproperty; seegetFilename().- Parameters:
value- the new value (notnull)
-
getHeartbeatMs
public int getHeartbeatMs()Specifies the interval, in milliseconds, betweenAbortCheckevent callbacks. These callbacks allow an application to cancel certain long-running operations before they finish.The default value is
0, which means noAbortCheckcallbacks are triggered.- Returns:
- the value of the
HeartbeatMsproperty
-
setHeartbeatMs
public void setHeartbeatMs(int value)Sets theHeartbeatMsproperty; seegetHeartbeatMs().- Parameters:
value- the new value
-
getLastModStr
Specifies the last-modified date/time to embed in the Gzip file when aCompress*method is called.The value must be provided as an RFC 822 formatted date/time string.
Example:
Sun, 12 Apr 2026 12:45:26 GMT
If this property is not set, the current system date/time is used automatically.
- Returns:
- the value of the
LastModStrproperty
-
setLastModStr
Sets theLastModStrproperty; seegetLastModStr().- Parameters:
value- the new value (notnull)
-
getUseCurrentDate
public boolean getUseCurrentDate()Controls the last-modified date/time assigned to files created byUncompress*methods.When set to
, the extracted file uses the current date/time instead of the date/time stored in the Gzip data.true- Returns:
- the value of the
UseCurrentDateproperty
-
setUseCurrentDate
public void setUseCurrentDate(boolean value)Sets theUseCurrentDateproperty; seegetUseCurrentDate().- Parameters:
value- the new value
-
compressBd
Compresses the contents of aBinDataobject in place, replacing the original data with Gzip-compressed data.- Parameters:
binDat- thebinDatargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
compressFile
Compresses a file and writes the result as a Gzip file, typically with a.gzextension.- Parameters:
srcPath- thesrcPathargument (notnull)destPath- thedestPathargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
compressFile2
Compresses a file and writes the result as a Gzip file, while allowing a different filename to be embedded inside the Gzip data.The
inFilenameparameter is the actual file on disk. Theparameter is the filename stored in the Gzip header and may be used by extraction tools as the output filename.srcPath- Parameters:
srcPath- thesrcPathargument (notnull)embeddedFilename- theembeddedFilenameargument (notnull)destPath- thedestPathargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
compressFileBd
Compresses a file and stores the resulting Gzip data in aBinDataobject.The compressed output is held in memory. The maximum compressed size is 4 GB.
- Parameters:
filePath- thefilePathargument (notnull)bd- thebdargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
compressSb
Compresses the text contained in aStringBuilderand writes the Gzip-compressed result to aBinDataobject.Before compression, the string is converted to bytes using the specified character set, such as
utf-8,iso-8859-1, orshift_JIS.- Parameters:
sb- thesbargument (notnull)charset- thecharsetargument (notnull)bd- thebdargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
compressStringENC
Compresses a string and returns the Gzip-compressed data as an encoded string.The input string is first converted to bytes using the specified character set. The compressed binary data is then encoded using the requested encoding, such as
base64,hex,url,base32, or quoted-printable.- Parameters:
inStr- theinStrargument (notnull)charset- thecharsetargument (notnull)encoding- theencodingargument (notnull)- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
compressStringToFile
Compresses a string and writes the resulting Gzip data to a file.The string is first converted to bytes using the character set specified by
destCharset.- Parameters:
inStr- theinStrargument (notnull)destCharset- thedestCharsetargument (notnull)destPath- thedestPathargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
getGzipInfo
Retrieves metadata stored in a Gzip file and returns the information in aJsonObject.The metadata is returned using the following JSON structure:
{ "extraData": "AAECAw==", "filename": "example.txt", "comment": "This is the comment" }- filename – the original filename embedded in the Gzip file (if present)
- comment – an optional descriptive comment (if present)
- extraData – optional additional binary data, returned as a Base64-encoded
string
Any of these fields may be omitted if they were not included when the Gzip file was created.
- Parameters:
filePath- thefilePathargument (notnull)json- thejsonargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
getGzipInfoBd
Retrieves metadata stored in Gzip data contained within aBinDataobject and returns the information in aJsonObject.The metadata is returned using the same JSON structure:
{ "extraData": "AAECAw==", "filename": "example.txt", "comment": "This is the comment" }- filename – the original filename embedded in the Gzip data (if present)
- comment – an optional descriptive comment (if present)
- extraData – optional additional binary data, returned as a Base64-encoded
string
Any of these fields may be omitted if they were not included when the Gzip data was created.
- Parameters:
bd- thebdargument (notnull)json- thejsonargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
isGzip
Checks whether the data contained in aBinDataobject is in Gzip format.Returns
if the data is Gzip-formatted, ortrueotherwise.false- Parameters:
bd- thebdargument (notnull)- Returns:
- the result; this method never throws
ChilkatException
-
setDt
Sets the last-modified date/time to embed in the Gzip file when aCompress*method is called.If no date/time is explicitly set, the current system date/time is used.
- Parameters:
dt- thedtargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
setExtraData
Sets optional extra binary data to include in the Gzip header when aCompress*method is called.The data is passed as an encoded string. Supported encodings include
base64,hex,ascii, and many others.- Parameters:
encodedData- theencodedDataargument (notnull)encoding- theencodingargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
unTarGz
Extracts a.tar.gzarchive to a directory.The Gzip decompression and TAR extraction are performed in streaming mode. No temporary files are created, and memory usage remains small and constant.
If
bNoAbsoluteis, absolute paths in the TAR archive are not allowed. This helps protect against extracting files to unsafe locations, such as system directories.true- Parameters:
gzPath- thegzPathargument (notnull)destDir- thedestDirargument (notnull)bNoAbsolute- thebNoAbsoluteargument- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
uncompressBd
Decompresses Gzip data contained in aBinDataobject in place, replacing the compressed data with the uncompressed data.- Parameters:
binDat- thebinDatargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
uncompressBdToFile
Decompresses Gzip data stored in aBinDataobject and writes the result to a file.- Parameters:
bd- thebdargument (notnull)filePath- thefilePathargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
uncompressFile
Decompresses a Gzip file and writes the result to the specified output path.The output filename is provided by the caller. The filename embedded in the Gzip data is not used.
- Parameters:
srcPath- thesrcPathargument (notnull)destPath- thedestPathargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
uncompressFileToString
Decompresses a Gzip file that contains text and returns the uncompressed text as a string.The
charsetparameter specifies the character encoding of the uncompressed text, such asutf-8,iso-8859-1,windows-1252,shift_JIS,big5, etc.- Parameters:
srcPath- thesrcPathargument (notnull)charset- thecharsetargument (notnull)- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
uncompressStringENC
Decompresses Gzip data provided as an encoded string and returns the uncompressed result as text.The input string is first decoded using the specified encoding, such as
base64,hex,url,base32, quoted-printable, etc. The decoded Gzip data is then decompressed and converted to text using the specified character set.- Parameters:
inStr- theinStrargument (notnull)charset- thecharsetargument (notnull)encoding- theencodingargument (notnull)- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
xfdlToXml
Converts encoded and compressed XFDL data to a decompressed XML string.XFDL data is often stored in an ASCII-safe compressed form, such as
asci-gziporasc-gzip. Despite the name, this does not always mean the data is a standard.gzfile. In some XFDL files, the compressed payload uses the raw deflate format rather than the full Gzip file format.Chilkat automatically detects the actual encoding and compression format used by the XFDL data. It decodes the ASCII/Base64 representation, determines whether the compressed data is Gzip or deflate, decompresses it correctly, and returns the resulting XML string.
This allows applications to call
XfdlToXmlwithout needing to manually distinguish between XFDL variants such as Base64+Gzip and Base64+Deflate.Returns the decoded and decompressed XML string.
XFDL (Extensible Forms Description Language) is an XML-based format used to define secure, interactive electronic forms—often including digital signatures and integrity protections—commonly used in government and enterprise applications.
- Parameters:
xfldData- thexfldDataargument (notnull)- Returns:
- the result (never
null) - 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
-