ZipCrc Chilkat2 Perl Reference Documentation

Chilkat2::ZipCrc

Current Version: 11.7.0

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

Chilkat.ZipCrc

Calculate ZIP-compatible CRC32 values for files, bytes, strings, and streamed data.

Chilkat.ZipCrc calculates the 32-bit CRC value used in ZIP file headers. It can compute CRC32 values for in-memory byte arrays, BinData, StringBuilder content, strings using a specified character encoding, and entire files. It also supports streaming CRC calculation, allowing applications to process large or incremental data in chunks without holding the full input in memory.

CRC32 for byte data

Use CalculateCrc to calculate a ZIP-style CRC32 value for an in-memory byte array.

BinData support

Use CrcBd when the input bytes are already stored in a Chilkat.BinData object.

String CRCs

Use CrcString or CrcSb to calculate CRC32 for text using a specific charset such as utf-8, utf-16, or windows-1252.

File CRCs

Use FileCrc to calculate the CRC32 of a file while streaming the file contents to keep memory usage small and constant.

Streaming calculation

Call BeginStream, add chunks with MoreData, and finish with EndStream when data arrives incrementally.

Hex formatting

Use ToHex to convert a 32-bit CRC integer into a readable hexadecimal string.

Common pattern: Use FileCrc for files, CalculateCrc or CrcBd for binary data already in memory, and CrcString or CrcSb for text where the byte representation depends on a charset. For large or chunked input, initialize the CRC with BeginStream, feed each block with MoreData, and call EndStream to obtain the final CRC32 value.
ZIP compatibility note: The CRC returned by this class is the same 32-bit CRC value that would appear in a ZIP file header for the same byte content. When calculating CRCs for strings, the selected charset matters because the CRC is computed over bytes, not abstract characters.

Object Creation

use Chilkat2;

my $zipCrc = Chilkat2::ZipCrc->new;
# or set properties (and RaiseError) at creation:
# my $zipCrc = Chilkat2::ZipCrc->new(VerboseLogging => 1, RaiseError => 1);
my $zipCrc = Chilkat2::ZipCrc->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/ZipCrc.pm file holds only this class's documentation (perldoc Chilkat2::ZipCrc).

Perl frees the object when its last reference goes (undef $zipCrc, 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::ZipCrc';): an object is a blessed hash reference whose hash is free for the subclass's own fields.

my $boolVal = $zipCrc->RaiseError;
$zipCrc->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::ZipCrc'), 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

DebugLogFilePath
# $strVal is a string
my $strVal = $zipCrc->DebugLogFilePath;
$zipCrc->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
LastErrorHtml
# $strVal is a string (read-only)
my $strVal = $zipCrc->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 = $zipCrc->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 = $zipCrc->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 = $zipCrc->LastMethodSuccess;
$zipCrc->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
VerboseLogging
# $boolVal is a boolean
my $boolVal = $zipCrc->VerboseLogging;
$zipCrc->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 = $zipCrc->Version;

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

More Information and Examples
top

Methods

BeginStream
$zipCrc->BeginStream();

Provides a way to calculate a CRC by streaming the data a chunk at a time. An application would start by calling BeginStream. Then it would add data by calling MoreData for each additional chunk. After the last chunk has been processed, the EndStream method is called to return the CRC.

Returns the object, so calls can be chained.

top
CrcBd
# $bd is a Chilkat2::BinData
# $n is an integer
my $n = $zipCrc->CrcBd($bd);
Introduced in version 9.5.0.75

Calculates a CRC32 for the bytes contained in $bd.

top
CrcSb
# $sb is a Chilkat2::StringBuilder
# $charset is a string
# $n is an integer
my $n = $zipCrc->CrcSb($sb, $charset);
Introduced in version 9.5.0.75

Calculates a CRC32 for the string contained in $sb. The $charset is the byte representation to be used for the $sb when calculating the CRC32. It can be utf-8, utf-16, windows-1252, iso-8859-1, or any of the character encodings (charsets) listed at the link below.

top
CrcString
# $str is a string
# $charset is a string
# $n is an integer
my $n = $zipCrc->CrcString($str, $charset);
Introduced in version 9.5.0.75

Calculates a CRC32 for a string. The $charset is the byte representation to be used for the $str when calculating the CRC32. It can be utf-8, utf-16, windows-1252, iso-8859-1, or any of the character encodings (charsets) listed at the link below.

top
EndStream
# $n is an integer
my $n = $zipCrc->EndStream();

Finalizes and returns the Zip CRC value calculated by calling BeginStream followed by multiple calls to MoreData.

top
FileCrc
# $path is a string
# $n is an integer
my $n = $zipCrc->FileCrc($path);

Calculates the CRC32 of a file. The data contained in the file is streamed for the calculation to keep the memory footprint small and constant. Returns the CRC32 of the file.

More Information and Examples
top
ToHex
# $crc is an integer
# $str is a string or undef
my $str = $zipCrc->ToHex($crc);

Converts a 32-bit integer to a hex string.

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

Events

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

$zipCrc->on(
    PercentDone  => sub { my ($pctDone) = @_; print "$pctDone%\n"; return 0 },   # a true return aborts
    ProgressInfo => sub { my ($name, $value) = @_; print "$name: $value\n" },
);
$zipCrc->on(Event => CODE, ...);

Installs the handlers given, replacing those installed before for the same events; undef instead of a CODE reference removes one, and $zipCrc->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 periodically during long operations; 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 $zipCrc 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
$zipCrc->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
$zipCrc->on(AbortCheck => sub { return time() > $deadline });
top
PercentDone
# handler: sub { my ($pctDone) = @_; ...; return $abort } -- a true return aborts
$zipCrc->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

$zipCrc->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) = @_; ... }
$zipCrc->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

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