UnixCompress Chilkat2 Perl Reference Documentation
Chilkat2::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
use Chilkat2;
my $unixCompress = Chilkat2::UnixCompress->new;
# or set properties (and RaiseError) at creation:
# my $unixCompress = Chilkat2::UnixCompress->new(VerboseLogging => 1, RaiseError => 1);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/UnixCompress.pm file holds only this class's documentation (perldoc Chilkat2::UnixCompress).
Perl frees the object when its last reference goes (undef $unixCompress, 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::UnixCompress';): an object is a blessed hash reference whose hash is free for the subclass's own fields.
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::UnixCompress'), 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
my $boolVal = $unixCompress->AbortCurrent;
$unixCompress->AbortCurrent($boolVal);
When set to 1, 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 0 when the next method is called. When the abort occurs, this property is reset to 0. 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
my $strVal = $unixCompress->DebugLogFilePath;
$unixCompress->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.
HeartbeatMs
my $intVal = $unixCompress->HeartbeatMs;
$unixCompress->HeartbeatMs($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
my $strVal = $unixCompress->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.
topLastErrorText
my $strVal = $unixCompress->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.
LastErrorXml
my $strVal = $unixCompress->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.
topLastMethodSuccess
my $boolVal = $unixCompress->LastMethodSuccess;
$unixCompress->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.
VerboseLogging
my $boolVal = $unixCompress->VerboseLogging;
$unixCompress->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.
Version
my $strVal = $unixCompress->Version;
Methods
CompressFile
# $destPath is a string
# $success is true or false
my $success = $unixCompress->CompressFile($inFilename, $destPath);
Compresses a file to create a UNIX compressed file (.Z). This compression uses the LZW (Lempel-Ziv-Welch) compression algorithm.
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.
CompressStringToFile
# $charset is a string
# $destPath is a string
# $success is true or false
my $success = $unixCompress->CompressStringToFile($inStr, $charset, $destPath);
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, 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.
UncompressFile
# $destPath is a string
# $success is true or false
my $success = $unixCompress->UncompressFile($inFilename, $destPath);
Uncompresses a .Z file. (This compression uses the LZW (Lempel-Ziv-Welch) compression algorithm.)
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.
UncompressFileToString
# $charset is a string
# $str is a string or undef
my $str = $unixCompress->UncompressFileToString($zFilename, $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 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.
UnTarZ
# $destDir is a string
# $bNoAbsolute is a boolean
# $success is true or false
my $success = $unixCompress->UnTarZ($zFilename, $destDir, $bNoAbsolute);
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, 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.
Events
All Chilkat methods are synchronous: the call returns when the work is done. During a long-running call, Chilkat2::UnixCompress reports progress to handlers installed with on, which takes Event => CODE pairs (closures, so a handler can use the variables around it):
$unixCompress->on(
PercentDone => sub { my ($pctDone) = @_; print "$pctDone%\n"; return 0 }, # a true return aborts
ProgressInfo => sub { my ($name, $value) = @_; print "$name: $value\n" },
);
$unixCompress->HeartbeatMs(250); # the AbortCheck handler is called 4 times per secondInstalls the handlers given, replacing those installed before for the same events; undef instead of a CODE reference removes one, and $unixCompress->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 $unixCompress itself makes a reference cycle: weaken the captured copy (Scalar::Util) or remove the handler when done.
AbortCheck
$unixCompress->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.
Example
my $deadline = time() + 30; # give up after 30 seconds
$unixCompress->HeartbeatMs(250); # call the handler 4 times per second
$unixCompress->on(AbortCheck => sub { return time() > $deadline });PercentDone
$unixCompress->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.
Example
$unixCompress->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
});ProgressInfo
$unixCompress->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.
Example
$unixCompress->on(ProgressInfo => sub {
my ($name, $value) = @_;
print "$name: $value\n";
});