UnixCompress Chilkat2 PHP Reference Documentation

UnixCompress

Current Version: 11.7.0

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

Chilkat.UnixCompress

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

Compress files, memory data, or strings into UNIX .Z format using LZW compression.

Uncompress .Z data

Expand UNIX-compressed data back to files, memory, byte arrays, or text depending on the application workflow.

File workflows

Compress and uncompress directly between input and output files when working with existing filesystem data.

Memory workflows

Work with compressed or uncompressed byte data in memory when temporary files are not desired.

String workflows

Compress and uncompress text using an explicit character set so string data round-trips correctly.

Unpack tar.Z archives

Use UnTarZ to decompress and extract legacy .tar.Z archives in a single workflow.

Common pattern: Use 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\UnixCompress;

try {
    $unixCompress = new UnixCompress();
    // ... use $unixCompress ...
} catch (\Chilkat2\Exception $e) {
    echo $e->getClass(), '::', $e->getMethod(), " failed\n";
    echo $e->getMessage(), "\n";              // the LastErrorText of the failed call
}
public function __construct(array $properties = [])

Creates the object. The optional array sets properties in order, as if assigned one by one (new UnixCompress(['VerboseLogging' => true, 'RaiseError' => false])); an unknown or read-only name throws Error. The class is defined by the chilkat2 extension (extension=chilkat2 in php.ini): there is no PHP file to include.

There is no close(): PHP frees the object when its last reference goes (unset($unixCompress), $unixCompress = null, or the end of the function or request). Objects returned by methods are independent and freed the same way. The class may be extended (class MyUnixCompress extends \Chilkat2\UnixCompress); objects cannot be cloned or serialized.

public bool $RaiseError;

When true (the default), a method that fails throws Chilkat2\Exception. When false, it returns false (or null) instead, and the reason is in LastErrorText. Objects returned by a method inherit the value of the object that returned them.

Errors

Chilkat2\Exception extends RuntimeException: getMessage() is the failed call's full LastErrorText, getClass() its class ('Chilkat2\UnixCompress') and getMethod() the method name. The object remains usable. Methods that look something up and find nothing return null without throwing (each such method says so), methods that answer a question (HasMember, IsUnlocked, ...) return a plain bool, and property reads and writes do not throw for Chilkat reasons.

Misuse always throws, whatever RaiseError is: a wrong argument type is a TypeError (exact types under declare(strict_types=1)), a missing or extra argument an ArgumentCountError, a string containing a NUL byte or an out-of-range integer a ValueError, and an unknown property (properties are case-sensitive) or a write to a read-only one an Error. Named arguments work with the parameter names shown.

Strings are passed and returned as UTF-8; binary data is a PHP string of bytes. Unlocking: call (new \Chilkat2\GlobalSettings())->UnlockBundle('...') once at startup.

Properties

AbortCurrent
public bool $AbortCurrent;
Introduced in version 9.5.0.58

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.)

top
DebugLogFilePath
public string $DebugLogFilePath;

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
HeartbeatMs
public int $HeartbeatMs;

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.

More Information and Examples
top
LastErrorHtml
public readonly string $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
public readonly string $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
public readonly string $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
public bool $LastMethodSuccess;

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.

top
VerboseLogging
public bool $VerboseLogging;

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.

top
Version
public readonly string $Version;

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

More Information and Examples
top

Methods

CompressFile
public function CompressFile(string $inFilename, string $destPath): bool

Compresses a file to create a UNIX compressed file (.Z). This compression uses the LZW (Lempel-Ziv-Welch) compression algorithm.

Returns true on success. On failure it throws Chilkat2\Exception (or, with RaiseError set to false, returns false). $e->getMessage() (or the object's LastErrorText) explains why, and the object remains usable.

top
CompressStringToFile
public function CompressStringToFile(string $inStr, string $charset, string $destPath): bool

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. On failure it throws Chilkat2\Exception (or, with RaiseError set to false, returns false). $e->getMessage() (or the object's LastErrorText) explains why, and the object remains usable.

More Information and Examples
top
UncompressFile
public function UncompressFile(string $inFilename, string $destPath): bool

Uncompresses a .Z file. (This compression uses the LZW (Lempel-Ziv-Welch) compression algorithm.)

Returns true on success. On failure it throws Chilkat2\Exception (or, with RaiseError set to false, returns false). $e->getMessage() (or the object's LastErrorText) explains why, and the object remains usable.

top
UncompressFileToString
public function UncompressFileToString(string $zFilename, string $charset): ?string

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.

On failure it throws Chilkat2\Exception (or, with RaiseError set to false, returns null). $e->getMessage() (or the object's LastErrorText) explains why, and the object remains usable.

More Information and Examples
top
UnTarZ
public function UnTarZ(string $zFilename, string $destDir, bool $bNoAbsolute): bool

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. On failure it throws Chilkat2\Exception (or, with RaiseError set to false, returns false). $e->getMessage() (or the object's LastErrorText) explains why, and the object remains usable.

top

Events

All Chilkat methods are synchronous: the call returns when the work is done. During a long-running call, UnixCompress reports progress to handlers installed with three methods, each taking any PHP callable (a closure, an arrow function, $this->method(...), an invokable object) and returning the object, so the calls chain:

$unixCompress->onPercentDone(function (int $pctDone): bool {
        echo "$pctDone%\n";
        return false;           // return true to abort the method in progress
    })
    ->onProgressInfo(function (string $name, string $value): void {
        echo "$name: $value\n";
    });
$unixCompress->HeartbeatMs = 250;   // the AbortCheck handler is called 4 times per second
public function onAbortCheck(?callable $callback): static
public function onPercentDone(?callable $callback): static
public function onProgressInfo(?callable $callback): static

Each installs the handler for one event, replacing the one installed before; null removes it. A callable that does not exist is a TypeError right away.

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 truthy return from the AbortCheck or PercentDone handler aborts the running method, which then fails in the usual way (Chilkat2\Exception, or false / null with RaiseError off). PHP's set_time_limit() does not interrupt a Chilkat call; an AbortCheck handler is how a script enforces its own limit.

Handlers run during the method call, in the same request. Inside a handler, the object's properties (such as $unixCompress->AbortCurrent = true) and other objects may be used, but calling a method of the same object throws Error. An exception thrown by a handler aborts the running method and propagates from it; the object remains usable. A closure that captures $unixCompress itself makes a reference cycle, which PHP's cycle collector frees.

AbortCheck
// handler: function (): bool -- return true to abort
public function onAbortCheck(?callable $callback): static

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

$deadline = microtime(true) + 30;                     // give up after 30 seconds
$unixCompress->HeartbeatMs = 250;                       // call the handler 4 times per second
$unixCompress->onAbortCheck(fn(): bool => microtime(true) > $deadline);
top
PercentDone
// handler: function (int $pctDone): bool -- return true to abort
public function onPercentDone(?callable $callback): static

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.

More Information and Examples

Example

$unixCompress->onPercentDone(function (int $pctDone): bool {
    // $pctDone ranges from 0 to 100.
    echo "Percent done: $pctDone\n";
    return false;   // return true to abort the method in progress
});
top
ProgressInfo
// handler: function (string $name, string $value): void
public function onProgressInfo(?callable $callback): static

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

$unixCompress->onProgressInfo(function (string $name, string $value): void {
    echo "$name: $value\n";
});
top