Class ZipCrc
- All Implemented Interfaces:
AutoCloseable
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.
</section>
Event-capable: long-running methods report progress through a ProgressListener installed
with setProgressListener(ProgressListener), onAbortCheck(BooleanSupplier),
onPercentDone(IntPredicate) or onProgressInfo(BiConsumer).
Instances hold a native Chilkat object: close them with ChilkatObject.close() (try-with-resources) when
done; an unclosed instance is freed when it becomes unreachable.
- See Also:
- Online reference
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidProvides a way to calculate a CRC by streaming the data a chunk at a time.longCalculates a CRC32 for the bytes contained inbd.longcrcSb(StringBuilder sb, String charset)Calculates a CRC32 for the string contained insb.longCalculates a CRC32 for a string.longFinalizes and returns the Zip CRC value calculated by calling BeginStream followed by multiple calls to MoreData.longCalculates the CRC32 of a file.Returns the installed listener, ornull.voidonAbortCheck(BooleanSupplier callback)Installs a listener whose only callback isabortCheck: called everyHeartbeatMsmilliseconds during a long-running method; returntrueto abort it.voidonPercentDone(IntPredicate callback)Installs a listener whose only callback ispercentDone: called with the completion percentage (0..100) as a long-running method progresses; returntrueto abort it.voidonProgressInfo(BiConsumer<String,String> callback)Installs a listener whose only callback isprogressInfo: called with a name/value pair of progress information as a long-running method proceeds.voidsetProgressListener(ProgressListener listener)Installs the listener that receives this object's progress events (abortCheck,percentDone,progressInfo) during its long-running methods; replaces any listener or lambda installed before;nullremoves it.toHex(long crc)Converts a 32-bit integer to a hex string.Methods inherited from class com.chilkatsoft.chilkat2.ChilkatObject
close, getDebugLogFilePath, getLastErrorHtml, getLastErrorText, getLastErrorXml, getLastMethodSuccess, getVerboseLogging, getVersion, isClosed, setDebugLogFilePath, setLastMethodSuccess, setVerboseLogging, toString
-
Constructor Details
-
ZipCrc
public ZipCrc()Creates a newZipCrcobject.
-
-
Method Details
-
beginStream
public void 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. -
crcBd
Calculates a CRC32 for the bytes contained inbd.- Parameters:
bd- thebdargument (notnull)- Returns:
- the result (see
ChilkatObject.getLastMethodSuccess()where the description mentions failure)
-
crcSb
Calculates a CRC32 for the string contained insb. Thecharsetis the byte representation to be used for thesbwhen 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.- Parameters:
sb- thesbargument (notnull)charset- thecharsetargument (notnull)- Returns:
- the result (see
ChilkatObject.getLastMethodSuccess()where the description mentions failure)
-
crcString
Calculates a CRC32 for a string. Thecharsetis the byte representation to be used for thestrwhen 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.- Parameters:
str- thestrargument (notnull)charset- thecharsetargument (notnull)- Returns:
- the result (see
ChilkatObject.getLastMethodSuccess()where the description mentions failure)
-
endStream
public long endStream()Finalizes and returns the Zip CRC value calculated by calling BeginStream followed by multiple calls to MoreData.- Returns:
- the result (see
ChilkatObject.getLastMethodSuccess()where the description mentions failure)
-
fileCrc
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.- Parameters:
path- thepathargument (notnull)- Returns:
- the result (see
ChilkatObject.getLastMethodSuccess()where the description mentions failure)
-
toHex
Converts a 32-bit integer to a hex string.- Parameters:
crc- thecrcargument (0 .. 4294967295)- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
setProgressListener
Installs the listener that receives this object's progress events (abortCheck,percentDone,progressInfo) during its long-running methods; replaces any listener or lambda installed before;nullremoves it.- Parameters:
listener- the listener, ornull- Throws:
IllegalStateException- if called from inside one of this object's own callbacks
-
getProgressListener
Returns the installed listener, ornull.- Returns:
- the listener installed with
setProgressListener(ProgressListener)or one of theon...methods
-
onAbortCheck
Installs a listener whose only callback isabortCheck: called everyHeartbeatMsmilliseconds during a long-running method; returntrueto abort it. Replaces any listener installed before.- Parameters:
callback- the callback, ornullto remove the listener
-
onPercentDone
Installs a listener whose only callback ispercentDone: called with the completion percentage (0..100) as a long-running method progresses; returntrueto abort it. Replaces any listener installed before.- Parameters:
callback- the callback, ornullto remove the listener
-
onProgressInfo
Installs a listener whose only callback isprogressInfo: called with a name/value pair of progress information as a long-running method proceeds. Replaces any listener installed before.- Parameters:
callback- the callback, ornullto remove the listener
-