UnixCompress Lua Reference Documentation
chilkat.UnixCompress
Current Version: 11.7.0
Markdown version of this page for AI tools (v 11.7.0) · all AI docs
Chilkat.UnixCompress
Compress files, memory data, or strings into UNIX
Expand UNIX-compressed data back to files, memory, byte arrays, or text
depending on the application workflow.
Compress and uncompress directly between input and output files when
working with existing filesystem data.
Work with compressed or uncompressed byte data in memory when temporary
files are not desired.
Compress and uncompress text using an explicit character set so string
data round-trips correctly.
Use
For an extended overview, see
UnixCompress Class Overview.
Compress, uncompress, and unpack legacy UNIX
.Z files.Chilkat.UnixCompress provides UNIX compress and
uncompress functionality for legacy .Z files. It
supports file-based, memory-based, and string-based workflows, uses LZW
compression, can unpack .tar.Z archives, and includes abort and
progress-related support for longer-running operations.
Compress to .Z
.Z
format using LZW compression.
Uncompress .Z data
File workflows
Memory workflows
String workflows
Unpack tar.Z archives
UnTarZ to decompress and extract legacy
.tar.Z archives in a single workflow.
UnixCompress when the data is in classic UNIX
.Z format or when a .tar.Z archive must be
unpacked. Use Chilkat.Gzip for .gz and
.tar.gz files, Chilkat.Bz2 for
.bz2 files, and Chilkat.Compression for raw
compression algorithms and general-purpose compression workflows.
Object Creation
local chilkat = require("chilkat")
local unix_compress = chilkat.UnixCompress.new()Creates the object (chilkat.UnixCompress() does the same). Lua's garbage collector frees it when it is no longer referenced; unix_compress:dispose() frees it at once, and in Lua 5.4 and later local unix_compress <close> = chilkat.UnixCompress.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 = unix_compress: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 (unix_compress.verbose_logging = true) and methods use the colon (unix_compress: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 = unix_compress.abort_current
unix_compress.abort_current = boolVal
When set to true, causes the currently running method to abort. Methods that always finish quickly (i.e.have no length file operations or network communications) are not affected. If no method is running, then this property is automatically reset to false when the next method is called. When the abort occurs, this property is reset to false. Both synchronous and asynchronous method calls can be aborted. (A synchronous method call could be aborted by setting this property from a separate thread.)
DebugLogFilePath
local strVal = unix_compress.debug_log_file_path
unix_compress.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.
HeartbeatMs
local intVal = unix_compress.heartbeat_ms
unix_compress.heartbeat_ms = intVal
The interval in milliseconds between each AbortCheck event callback, which enables an application to abort certain method calls before they complete. By default, HeartbeatMs is set to 0, meaning no AbortCheck event callbacks will trigger.
LastErrorHtml
local strVal = unix_compress.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 = unix_compress.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 = unix_compress.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 = unix_compress.last_method_success
unix_compress.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.
VerboseLogging
local boolVal = unix_compress.verbose_logging
unix_compress.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 = unix_compress.version
Methods
CompressFile
-- dest_path is a string
-- ok is true, or nil and err on failure
local ok, err = unix_compress:compress_file(in_filename, dest_path)
Compresses a file to create a UNIX compressed file (.Z). This compression uses the LZW (Lempel-Ziv-Welch) compression algorithm.
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.
CompressStringToFile
-- charset is a string
-- dest_path is a string
-- ok is true, or nil and err on failure
local ok, err = unix_compress:compress_string_to_file(in_str, charset, dest_path)
Unix compresses and creates a .Z file from string data. The charset specifies the character encoding used for the byte representation of the characters when compressed. The charset can be any one of a large number of character encodings, such as utf-8, iso-8859-1, Windows-1252, ucs-2, etc.
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 = unix_compress:uncompress_file(in_filename, dest_path)
Uncompresses a .Z file. (This compression uses the LZW (Lempel-Ziv-Welch) compression algorithm.)
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 = unix_compress:uncompress_file_to_string(z_filename, charset)
Uncompresses a .Z file that contains a text file. The contents of the text file are returned as a string. The character encoding of the text file is specified by charset. Typical charsets are iso-8859-1, utf-8, 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.
UnTarZ
-- dest_dir is a string
-- b_no_absolute is a boolean
-- ok is true, or nil and err on failure
local ok, err = unix_compress:un_tar_z(z_filename, dest_dir, b_no_absolute)
Unpacks a .tar.Z file. The decompress and untar occur in streaming mode. There are no temporary files and the memory footprint is constant (and small) while untarring.
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.
Events
All Chilkat methods are synchronous: the call returns when the work is done. During a long-running call, chilkat.UnixCompress 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):
unix_compress:set_event_handler({
percent_done = function(pct) print(pct .. "%") end, -- return true to abort
progress_info = function(name, value) print(name, value) end,
})
unix_compress.heartbeat_ms = 250 -- abort_check is called 4 times per secondInstalls the handlers, replacing any installed before; unix_compress: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
unix_compress: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
unix_compress.heartbeat_ms = 250 -- call the handler 4 times per second
unix_compress:set_event_handler({ abort_check = function() return os.time() > deadline end })PercentDone
unix_compress: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
unix_compress: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
unix_compress: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
unix_compress:set_event_handler({
progress_info = function(name, value) print(name .. ": " .. value) end,
})