ZipCrc Lua Reference Documentation
chilkat.ZipCrc
Current Version: 11.7.0
Markdown version of this page for AI tools (v 11.7.0) · all AI docs
Chilkat.ZipCrc
Use
Use
Use
Use
Call
Use 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
CalculateCrc to calculate a ZIP-style CRC32 value for
an in-memory byte array.
BinData support
CrcBd when the input bytes are already stored in a
Chilkat.BinData object.
String CRCs
CrcString or CrcSb to calculate CRC32 for
text using a specific charset such as utf-8,
utf-16, or windows-1252.
File CRCs
FileCrc to calculate the CRC32 of a file while streaming
the file contents to keep memory usage small and constant.
Streaming calculation
BeginStream, add chunks with MoreData, and
finish with EndStream when data arrives incrementally.
Hex formatting
ToHex to convert a 32-bit CRC integer into a readable
hexadecimal string.
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.
Object Creation
local chilkat = require("chilkat")
local zip_crc = chilkat.ZipCrc.new()Creates the object (chilkat.ZipCrc() does the same). Lua's garbage collector frees it when it is no longer referenced; zip_crc:dispose() frees it at once, and in Lua 5.4 and later local zip_crc <close> = chilkat.ZipCrc.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 = zip_crc: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 (zip_crc.verbose_logging = true) and methods use the colon (zip_crc: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
DebugLogFilePath
local strVal = zip_crc.debug_log_file_path
zip_crc.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.
LastErrorHtml
local strVal = zip_crc.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 = zip_crc.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 = zip_crc.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 = zip_crc.last_method_success
zip_crc.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 = zip_crc.verbose_logging
zip_crc.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 = zip_crc.version
Methods
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.
topCrcBd
Calculates a CRC32 for the bytes contained in bd.
CrcSb
-- charset is a string
-- n is an integer
local n = zip_crc:crc_sb(sb, charset)
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.
CrcString
-- charset is a string
-- n is an integer
local n = zip_crc:crc_string(str, charset)
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.
EndStream
local n = zip_crc:end_stream()
Finalizes and returns the Zip CRC value calculated by calling BeginStream followed by multiple calls to MoreData.
topFileCrc
-- n is an integer
local n = zip_crc:file_crc(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.
ToHex
-- str is a string, or nil and err on failure
local str, err = zip_crc:to_hex(crc)
Converts a 32-bit integer to a hex string.
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.ZipCrc 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):
zip_crc:set_event_handler({
percent_done = function(pct) print(pct .. "%") end, -- return true to abort
progress_info = function(name, value) print(name, value) end,
})Installs the handlers, replacing any installed before; zip_crc: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 periodically during long operations; 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
zip_crc: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
zip_crc:set_event_handler({ abort_check = function() return os.time() > deadline end })PercentDone
zip_crc: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
zip_crc: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
zip_crc: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
zip_crc:set_event_handler({
progress_info = function(name, value) print(name .. ": " .. value) end,
})