Gzip Chilkat2 Perl Reference Documentation

Chilkat2::Gzip

Current Version: 11.7.0

Markdown version of this page for AI tools (v 11.7.0) · all AI docs

Chilkat.Gzip

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.

Object Creation

use Chilkat2;

my $gzip = Chilkat2::Gzip->new;
# or set properties (and RaiseError) at creation:
# my $gzip = Chilkat2::Gzip->new(VerboseLogging => 1, RaiseError => 1);
my $gzip = Chilkat2::Gzip->new(Property => $value, ...);

Creates the object. The optional pairs set writable properties (and RaiseError) in order, as if set one by one; an unknown or read-only name croaks. use Chilkat2; loads every class; the Chilkat2/Gzip.pm file holds only this class's documentation (perldoc Chilkat2::Gzip).

Perl frees the object when its last reference goes (undef $gzip, or the variable going out of scope); there is nothing to close. Objects returned by methods are independent and freed the same way. The class may be subclassed (use parent -norequire, 'Chilkat2::Gzip';): an object is a blessed hash reference whose hash is free for the subclass's own fields.

my $boolVal = $gzip->RaiseError;
$gzip->RaiseError($boolVal);

When false (the default), a method that fails returns false (or undef), and the reason is in LastErrorText. When true, it dies with a Chilkat2::Error instead, as DBI does with its RaiseError. Objects returned by a method inherit the value of the object that returned them.

Errors

A Chilkat2::Error stringifies to the failed call's full LastErrorText, and has the methods text, class ('Chilkat2::Gzip'), method (the method name) and object. Methods that look something up and find nothing return undef without dying (each such method says so), methods that answer a question (HasMember, IsUnlocked, ...) return a plain boolean, and property reads and writes never fail for Chilkat reasons.

Misuse always croaks, with your file and line, whatever RaiseError is: a wrong number of arguments, a reference or undef where a string or number is expected, an object of the wrong class, an out-of-range integer, a string containing a NUL character, a wide character in a byte string, or a write to a read-only property.

Strings are Perl character strings in both directions (length, regexes and :encoding layers see characters); binary data (BinData) is a byte string. Unlocking: call Chilkat2::Global->new->UnlockBundle('...') once at startup.

Properties

AbortCurrent
# $boolVal is a boolean
my $boolVal = $gzip->AbortCurrent;
$gzip->AbortCurrent($boolVal);
Introduced in version 9.5.0.58

Set this property to 1 to 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.

If no method is currently running, the property is automatically reset to 0 when the next method begins. After an abort occurs, it is also reset to 0. Both synchronous and asynchronous operations can be aborted. For synchronous calls, another thread must set this property.

top
Comment
# $strVal is a string
my $strVal = $gzip->Comment;
$gzip->Comment($strVal);

An optional comment to embed in the Gzip file when a Compress* method is called.

More Information and Examples
top
CompressionLevel
# $intVal is an integer
my $intVal = $gzip->CompressionLevel;
$gzip->CompressionLevel($intVal);
Introduced in version 9.5.0.50

Controls the compression level used when creating Gzip data. The value can range from 0 to 9.

  • 0 = no compression
  • 9 = 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.

More Information and Examples
top
DebugLogFilePath
# $strVal is a string
my $strVal = $gzip->DebugLogFilePath;
$gzip->DebugLogFilePath($strVal);

If set to a file path, this property logs the LastErrorText of each Chilkat method or property call to the specified file. This logging helps identify the context and history of Chilkat calls leading up to any crash or hang, aiding in debugging.

Enabling the VerboseLogging property provides more detailed information. This property is mainly used for debugging rare instances where a Chilkat method call causes a hang or crash, which should generally not happen.

Possible causes of hangs include:

  • A timeout property set to 0, indicating an infinite timeout.
  • A hang occurring within an event callback in the application code.
  • An internal bug in the Chilkat code causing the hang.

More Information and Examples
top
Filename
# $strVal is a string
my $strVal = $gzip->Filename;
$gzip->Filename($strVal);

The filename to embed in the Gzip file when a Compress* method is called. Some Gzip extraction tools use this embedded filename as the default output filename.

More Information and Examples
top
HeartbeatMs
# $intVal is an integer
my $intVal = $gzip->HeartbeatMs;
$gzip->HeartbeatMs($intVal);

Specifies the interval, in milliseconds, between AbortCheck event callbacks. These callbacks allow an application to cancel certain long-running operations before they finish.

The default value is 0, which means no AbortCheck callbacks are triggered.

More Information and Examples
top
LastErrorHtml
# $strVal is a string (read-only)
my $strVal = $gzip->LastErrorHtml;

Provides HTML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastErrorText
# $strVal is a string (read-only)
my $strVal = $gzip->LastErrorText;

Provides plain text information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastErrorXml
# $strVal is a string (read-only)
my $strVal = $gzip->LastErrorXml;

Provides XML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastMethodSuccess
# $boolVal is a boolean
my $boolVal = $gzip->LastMethodSuccess;
$gzip->LastMethodSuccess($boolVal);

Indicates the success or failure of the most recent method call: 1 means success, 0 means failure. This property remains unchanged by property setters or getters. This method is present to address challenges in checking for null or Nothing returns in certain programming languages. Note: This property does not apply to methods that return integer values or to boolean-returning methods where the boolean does not indicate success or failure.

top
LastModStr
# $strVal is a string
my $strVal = $gzip->LastModStr;
$gzip->LastModStr($strVal);

Specifies the last-modified date/time to embed in the Gzip file when a Compress* 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.

More Information and Examples
top
UseCurrentDate
# $boolVal is a boolean
my $boolVal = $gzip->UseCurrentDate;
$gzip->UseCurrentDate($boolVal);

Controls the last-modified date/time assigned to files created by Uncompress* methods.

When set to 1, the extracted file uses the current date/time instead of the date/time stored in the Gzip data.

top
VerboseLogging
# $boolVal is a boolean
my $boolVal = $gzip->VerboseLogging;
$gzip->VerboseLogging($boolVal);

If set to 1, then the contents of LastErrorText (or LastErrorXml, or LastErrorHtml) may contain more verbose information. The default value is 0. Verbose logging should only be used for debugging. The potentially large quantity of logged information may adversely affect peformance.

top
Version
# $strVal is a string (read-only)
my $strVal = $gzip->Version;

Version of the component/library, such as "10.1.0"

More Information and Examples
top

Methods

CompressBd
# $binDat is a Chilkat2::BinData
# $success is true or false
my $success = $gzip->CompressBd($binDat);
Introduced in version 9.5.0.67

Compresses the contents of a BinData object in place, replacing the original data with Gzip-compressed data.

Returns true on success, false on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.

More Information and Examples
top
CompressFile
# $srcPath is a string
# $destPath is a string
# $success is true or false
my $success = $gzip->CompressFile($srcPath, $destPath);

Compresses a file and writes the result as a Gzip file, typically with a .gz extension.

Returns true on success, false on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.

More Information and Examples
top
CompressFile2
# $srcPath is a string
# $embeddedFilename is a string
# $destPath is a string
# $success is true or false
my $success = $gzip->CompressFile2($srcPath, $embeddedFilename, $destPath);

Compresses a file and writes the result as a Gzip file, while allowing a different filename to be embedded inside the Gzip data.

The inFilename parameter is the actual file on disk. The $srcPath parameter is the filename stored in the Gzip header and may be used by extraction tools as the output filename.

Returns true on success, false on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.

More Information and Examples
top
CompressFileBd
# $filePath is a string
# $bd is a Chilkat2::BinData
# $success is true or false
my $success = $gzip->CompressFileBd($filePath, $bd);
Introduced in version 11.0.0

Compresses a file and stores the resulting Gzip data in a BinData object.

The compressed output is held in memory. The maximum compressed size is 4 GB.

Returns true on success, false on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.

top
CompressSb
# $sb is a Chilkat2::StringBuilder
# $charset is a string
# $bd is a Chilkat2::BinData
# $success is true or false
my $success = $gzip->CompressSb($sb, $charset, $bd);
Introduced in version 11.0.0

Compresses the text contained in a StringBuilder and writes the Gzip-compressed result to a BinData object.

Before compression, the string is converted to bytes using the specified character set, such as utf-8, iso-8859-1, or shift_JIS.

Returns true on success, false on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.

top
CompressStringENC
# $inStr is a string
# $charset is a string
# $encoding is a string
# $str is a string or undef
my $str = $gzip->CompressStringENC($inStr, $charset, $encoding);

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.

Returns undef on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.

top
CompressStringToFile
# $inStr is a string
# $destCharset is a string
# $destPath is a string
# $success is true or false
my $success = $gzip->CompressStringToFile($inStr, $destCharset, $destPath);

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.

Returns true on success, false on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.

top
GetGzipInfo
# $filePath is a string
# $json is a Chilkat2::JsonObject
# $success is true or false
my $success = $gzip->GetGzipInfo($filePath, $json);
Introduced in version 11.5.0

Retrieves metadata stored in a Gzip file and returns the information in a JsonObject.

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.

Returns true on success, false on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.

More Information and Examples
top
GetGzipInfoBd
# $bd is a Chilkat2::BinData
# $json is a Chilkat2::JsonObject
# $success is true or false
my $success = $gzip->GetGzipInfoBd($bd, $json);
Introduced in version 11.5.0

Retrieves metadata stored in Gzip data contained within a BinData object and returns the information in a JsonObject.

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.

Returns true on success, false on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.

top
IsGzip
# $bd is a Chilkat2::BinData
# $flag is true or false
my $flag = $gzip->IsGzip($bd);
Introduced in version 11.0.0

Checks whether the data contained in a BinData object is in Gzip format.

Returns 1 if the data is Gzip-formatted, or 0 otherwise.

Never dies: false is an answer, not a failure.

top
SetDt
# $dt is a Chilkat2::DateTime
# $success is true or false
my $success = $gzip->SetDt($dt);

Sets the last-modified date/time to embed in the Gzip file when a Compress* method is called.

If no date/time is explicitly set, the current system date/time is used.

Returns true on success, false on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.

More Information and Examples
top
SetExtraData
# $encodedData is a string
# $encoding is a string
# $success is true or false
my $success = $gzip->SetExtraData($encodedData, $encoding);
Introduced in version 11.0.0

Sets optional extra binary data to include in the Gzip header when a Compress* method is called.

The data is passed as an encoded string. Supported encodings include base64, hex, ascii, and many others.

Returns true on success, false on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.

top
UncompressBd
# $binDat is a Chilkat2::BinData
# $success is true or false
my $success = $gzip->UncompressBd($binDat);
Introduced in version 9.5.0.67

Decompresses Gzip data contained in a BinData object in place, replacing the compressed data with the uncompressed data.

Returns true on success, false on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.

More Information and Examples
top
UncompressBdToFile
# $bd is a Chilkat2::BinData
# $filePath is a string
# $success is true or false
my $success = $gzip->UncompressBdToFile($bd, $filePath);
Introduced in version 11.0.0

Decompresses Gzip data stored in a BinData object and writes the result to a file.

Returns true on success, false on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.

top
UncompressFile
# $srcPath is a string
# $destPath is a string
# $success is true or false
my $success = $gzip->UncompressFile($srcPath, $destPath);

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.

Returns true on success, false on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.

top
UncompressFileToString
# $srcPath is a string
# $charset is a string
# $str is a string or undef
my $str = $gzip->UncompressFileToString($srcPath, $charset);

Decompresses a Gzip file that contains text and returns the uncompressed text as a string.

The charset parameter specifies the character encoding of the uncompressed text, such as utf-8, iso-8859-1, windows-1252, shift_JIS, big5, etc.

Returns undef on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.

top
UncompressStringENC
# $inStr is a string
# $charset is a string
# $encoding is a string
# $str is a string or undef
my $str = $gzip->UncompressStringENC($inStr, $charset, $encoding);

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.

Returns undef on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.

top
UnTarGz
# $gzPath is a string
# $destDir is a string
# $bNoAbsolute is a boolean
# $success is true or false
my $success = $gzip->UnTarGz($gzPath, $destDir, $bNoAbsolute);

Extracts a .tar.gz archive 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 bNoAbsolute is 1, absolute paths in the TAR archive are not allowed. This helps protect against extracting files to unsafe locations, such as system directories.

Returns true on success, false on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.

More Information and Examples
top
XfdlToXml
# $xfldData is a string
# $str is a string or undef
my $str = $gzip->XfdlToXml($xfldData);

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-gzip or asc-gzip. Despite the name, this does not always mean the data is a standard .gz file. 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 XfdlToXml without 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.

Returns undef on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.

More Information and Examples
top

Events

All Chilkat methods are synchronous: the call returns when the work is done. During a long-running call, Chilkat2::Gzip reports progress to handlers installed with on, which takes Event => CODE pairs (closures, so a handler can use the variables around it):

$gzip->on(
    PercentDone  => sub { my ($pctDone) = @_; print "$pctDone%\n"; return 0 },   # a true return aborts
    ProgressInfo => sub { my ($name, $value) = @_; print "$name: $value\n" },
);
$gzip->HeartbeatMs(250);   # the AbortCheck handler is called 4 times per second
$gzip->on(Event => CODE, ...);

Installs the handlers given, replacing those installed before for the same events; undef instead of a CODE reference removes one, and $gzip->on() with no arguments returns the installed handlers as a list of pairs. The event names are checked: a misspelled one croaks, and nothing is installed.

The AbortCheck handler is called at the interval set by the HeartbeatMs property (0, the default, disables it); PercentDone is called when an operation's completion percentage is known; ProgressInfo delivers named progress values. A true return from the AbortCheck or PercentDone handler aborts the running method, which then fails in the usual way (false or undef, or a Chilkat2::Error under RaiseError).

Handlers run during the method call, on the thread that called it. Inside a handler, other objects may be used, but calling a method of the same object (or its on) croaks. A die in a handler aborts the running method and is re-thrown from the method call; the object remains usable. A handler that uses $gzip itself makes a reference cycle: weaken the captured copy (Scalar::Util) or remove the handler when done.

AbortCheck
# handler: sub { ...; return $abort } -- a true return aborts the method in progress
$gzip->on(AbortCheck => sub { ... });

Enables a method call to be aborted by triggering the AbortCheck event at intervals defined by the HeartbeatMs property. If HeartbeatMs is set to its default value of 0, no events will occur. For instance, set HeartbeatMs to 200 to trigger 5 AbortCheck events per second.

More Information and Examples

Example

my $deadline = time() + 30;                          # give up after 30 seconds
$gzip->HeartbeatMs(250);                      # call the handler 4 times per second
$gzip->on(AbortCheck => sub { return time() > $deadline });
top
PercentDone
# handler: sub { my ($pctDone) = @_; ...; return $abort } -- a true return aborts
$gzip->on(PercentDone => sub { ... });

This provides the percentage completion for any method involving network communications or time-consuming processing, assuming the progress can be measured as a percentage. This event is triggered only when it's possible and logical to express the operation's progress as a percentage. The pctDone argument will range from 1 to 100. For methods that finish quickly, the number of PercentDone callbacks may vary, but the final callback will have pctDone equal to 100. For longer operations, callbacks will not exceed one per percentage point (e.g., 1, 2, 3, ..., 98, 99, 100).

The PercentDone callback also acts as an AbortCheck event. For fast methods where PercentDone fires, an AbortCheck event may not trigger since the PercentDone callback already provides an opportunity to abort. For longer operations, where time between PercentDone callbacks is extended, AbortCheck callbacks enable more responsive operation termination.

To abort the operation, set the abort output argument to 1. This will cause the method to terminate and return a failure status or corresponding failure value.

More Information and Examples

Example

$gzip->on(PercentDone => sub {
    my ($pctDone) = @_;     # 0 to 100
    print "Percent done: $pctDone\n";
    return 0;               # return a true value to abort the method in progress
});
top
ProgressInfo
# handler: sub { my ($name, $value) = @_; ... }
$gzip->on(ProgressInfo => sub { ... });

This event callback provides tag name/value pairs that detail what occurs during a method call. To discover existing tag names, create code to handle the event, emit the pairs, and review them. Most tag names are self-explanatory.

Note: Some Chilkat methods don't fire any ProgressInfo events.

More Information and Examples

Example

$gzip->on(ProgressInfo => sub {
    my ($name, $value) = @_;
    print "$name: $value\n";
});
top