Gzip Lua Reference Documentation
chilkat.Gzip
Current Version: 11.7.0
Markdown version of this page for AI tools (v 11.7.0) · all AI docs
Chilkat.Gzip
Compress files to
Work with byte arrays and
Compress and uncompress strings with explicit charset handling for text
data that must round-trip correctly.
Handle GZIP data represented as Base64, hex, or other supported encoded
forms when binary data must be carried as text.
Set or inspect metadata such as original filename, modified time,
comments, and extra data stored in the GZIP header.
Extract
For an extended overview, see
Gzip Class Overview.
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
.gz, uncompress .gz files,
and write decompressed output directly to disk.
Memory-based data
Chilkat.BinData for applications
that need compression or decompression without temporary files.
String workflows
Encoded GZIP data
GZIP metadata
Tar-gzip and XFDL
.tar.gz archives and decode XFDL content that uses
gzip-related encodings.
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
local chilkat = require("chilkat")
local gzip = chilkat.Gzip.new()Creates the object (chilkat.Gzip() does the same). Lua's garbage collector frees it when it is no longer referenced; gzip:dispose() frees it at once, and in Lua 5.4 and later local gzip <close> = chilkat.Gzip.new() frees it when the block ends. Calling a method of a disposed object raises an error.
Errors
A method that fails returns nil followed by the error text, as io.open does: local body, err = gzip:method(...), where err is the object's last_error_text. Wrap the call in assert() to raise an error with that text instead. Methods that answer a question return true or false, numbers are returned as they are (check last_method_success where the description says a value means failure), and property reads and writes never fail for Chilkat reasons.
Misuse raises a Lua error: a wrong argument type or a missing argument (bad argument #1 to ...), an unknown property name, a write to a read-only property, an out-of-range integer, or an object of the wrong class.
Names are Chilkat's in snake_case: QuickGetStr is quick_get_str, LastErrorText is last_error_text. Properties are fields (gzip.verbose_logging = true) and methods use the colon (gzip:method(...)). Strings are UTF-8; binary data is a Lua string of bytes. Indexes are 0-based, as in every Chilkat API. Unlocking: call chilkat.unlock_bundle("...") once at startup.
Properties
AbortCurrent
local boolVal = gzip.abort_current
gzip.abort_current = boolVal
Set this property to 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.
true
If no method is currently running, the property is automatically reset to
when the next method begins. After an abort occurs, it is also reset to false.
Both synchronous and asynchronous operations can be aborted. For synchronous calls, another
thread must set this property.
false
Comment
local strVal = gzip.comment
gzip.comment = strVal
An optional comment to embed in the Gzip file when a Compress* method is called.
CompressionLevel
local intVal = gzip.compression_level
gzip.compression_level = intVal
Controls the compression level used when creating Gzip data. The value can range from
0 to 9.
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.
DebugLogFilePath
local strVal = gzip.debug_log_file_path
gzip.debug_log_file_path = 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.
Filename
local 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.
HeartbeatMs
local intVal = gzip.heartbeat_ms
gzip.heartbeat_ms = 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.
LastErrorHtml
local strVal = gzip.last_error_html
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.
topLastErrorText
local strVal = gzip.last_error_text
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.
LastErrorXml
local strVal = gzip.last_error_xml
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.
topLastMethodSuccess
local boolVal = gzip.last_method_success
gzip.last_method_success = boolVal
Indicates the success or failure of the most recent method call: true means success, false 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.
LastModStr
local strVal = gzip.last_mod_str
gzip.last_mod_str = 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.
UseCurrentDate
local boolVal = gzip.use_current_date
gzip.use_current_date = boolVal
Controls the last-modified date/time assigned to files created by Uncompress* methods.
When set to , the extracted file uses the current date/time instead of the
date/time stored in the Gzip data.
true
VerboseLogging
local boolVal = gzip.verbose_logging
gzip.verbose_logging = boolVal
If set to true, then the contents of LastErrorText (or LastErrorXml, or LastErrorHtml) may contain more verbose information. The default value is false. Verbose logging should only be used for debugging. The potentially large quantity of logged information may adversely affect peformance.
Version
local strVal = gzip.version
Methods
CompressBd
-- ok is true, or nil and err on failure
local ok, err = gzip:compress_bd(bin_dat)
Compresses the contents of a BinData object in place, replacing the original data
with Gzip-compressed data.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
CompressFile
-- dest_path is a string
-- ok is true, or nil and err on failure
local ok, err = gzip:compress_file(src_path, dest_path)
Compresses a file and writes the result as a Gzip file, typically with a .gz extension.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
CompressFile2
-- embedded_filename is a string
-- dest_path is a string
-- ok is true, or nil and err on failure
local ok, err = gzip:compress_file2(src_path, embedded_filename, dest_path)
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
parameter is the filename stored in the Gzip header and may be used
by extraction tools as the output filename.
src_path
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
CompressFileBd
-- bd is a chilkat.BinData
-- ok is true, or nil and err on failure
local ok, err = gzip:compress_file_bd(file_path, bd)
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, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
CompressSb
-- charset is a string
-- bd is a chilkat.BinData
-- ok is true, or nil and err on failure
local ok, err = gzip:compress_sb(sb, charset, bd)
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, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
CompressStringENC
-- charset is a string
-- encoding is a string
-- str is a string, or nil and err on failure
local str, err = gzip:compress_string_enc(in_str, 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 the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
CompressStringToFile
-- dest_charset is a string
-- dest_path is a string
-- ok is true, or nil and err on failure
local ok, err = gzip:compress_string_to_file(in_str, dest_charset, dest_path)
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, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
GetGzipInfo
-- json is a chilkat.JsonObject
-- ok is true, or nil and err on failure
local ok, err = gzip:get_gzip_info(file_path, json)
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, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
GetGzipInfoBd
-- json is a chilkat.JsonObject
-- ok is true, or nil and err on failure
local ok, err = gzip:get_gzip_info_bd(bd, json)
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, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
IsGzip
Checks whether the data contained in a BinData object is in Gzip format.
Returns if the data is Gzip-formatted, or true otherwise.
false
Returns true or false: an answer, not a success flag.
SetDt
-- ok is true, or nil and err on failure
local ok, err = gzip:set_dt(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, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
SetExtraData
-- encoding is a string
-- ok is true, or nil and err on failure
local ok, err = gzip:set_extra_data(encoded_data, encoding)
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, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
UncompressBd
-- ok is true, or nil and err on failure
local ok, err = gzip:uncompress_bd(bin_dat)
Decompresses Gzip data contained in a BinData object in place, replacing the
compressed data with the uncompressed data.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
UncompressBdToFile
-- file_path is a string
-- ok is true, or nil and err on failure
local ok, err = gzip:uncompress_bd_to_file(bd, file_path)
Decompresses Gzip data stored in a BinData object and writes the result to a file.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
UncompressFile
-- dest_path is a string
-- ok is true, or nil and err on failure
local ok, err = gzip:uncompress_file(src_path, dest_path)
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, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
UncompressFileToString
-- charset is a string
-- str is a string, or nil and err on failure
local str, err = gzip:uncompress_file_to_string(src_path, 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 the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
UncompressStringENC
-- charset is a string
-- encoding is a string
-- str is a string, or nil and err on failure
local str, err = gzip:uncompress_string_enc(in_str, 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 the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
UnTarGz
-- dest_dir is a string
-- b_no_absolute is a boolean
-- ok is true, or nil and err on failure
local ok, err = gzip:un_tar_gz(gz_path, dest_dir, b_no_absolute)
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 , absolute paths in the TAR archive are not
allowed. This helps protect against extracting files to unsafe locations, such as system
directories.
true
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
XfdlToXml
-- str is a string, or nil and err on failure
local str, err = gzip:xfdl_to_xml(xfld_data)
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 the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
Events
All Chilkat methods are synchronous: the call returns when the work is done. During a long-running call, chilkat.Gzip reports progress to the functions given to set_event_handler, in a table keyed by event name (plain functions, not methods: closures carry whatever state they need):
gzip:set_event_handler({
percent_done = function(pct) print(pct .. "%") end, -- return true to abort
progress_info = function(name, value) print(name, value) end,
})
gzip.heartbeat_ms = 250 -- abort_check is called 4 times per secondInstalls the handlers, replacing any installed before; gzip:set_event_handler(nil) removes them. The event names are checked when the table is installed: a misspelled one raises an error.
The abort_check handler is called at the interval set by the heartbeat_ms property (0, the default, disables it); percent_done is called when an operation's completion percentage is known; progress_info delivers named progress values. A true return from abort_check or percent_done aborts the running method, which then fails in the usual way (nil, err). An error raised in a handler aborts the method and is raised again from the method call; the object remains usable.
AbortCheck
gzip:set_event_handler({ abort_check = function() ... end })
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.
Example
local deadline = os.time() + 30 -- give up after 30 seconds
gzip.heartbeat_ms = 250 -- call the handler 4 times per second
gzip:set_event_handler({ abort_check = function() return os.time() > deadline end })PercentDone
gzip:set_event_handler({ percent_done = function(pct_done) ... end })
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 true. This will cause the method to terminate and return a failure status or corresponding failure value.
Example
gzip:set_event_handler({
percent_done = function(pct_done) -- 0 to 100
print("Percent done: " .. pct_done)
return false -- return true to abort the method in progress
end,
})ProgressInfo
gzip:set_event_handler({ progress_info = function(name, value) ... end })
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.
Example
gzip:set_event_handler({
progress_info = function(name, value) print(name .. ": " .. value) end,
})