Class ZipCrc

All Implemented Interfaces:
AutoCloseable

public class ZipCrc extends ChilkatObject
<section class="zipcrc-summary">

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 Details

    • ZipCrc

      public ZipCrc()
      Creates a new ZipCrc object.
  • 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

      public long crcBd​(BinData bd)
      Calculates a CRC32 for the bytes contained in bd.
      Parameters:
      bd - the bd argument (not null)
      Returns:
      the result (see ChilkatObject.getLastMethodSuccess() where the description mentions failure)
    • crcSb

      public long crcSb​(StringBuilder sb, String 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.
      Parameters:
      sb - the sb argument (not null)
      charset - the charset argument (not null)
      Returns:
      the result (see ChilkatObject.getLastMethodSuccess() where the description mentions failure)
    • crcString

      public long crcString​(String str, String 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.
      Parameters:
      str - the str argument (not null)
      charset - the charset argument (not null)
      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

      public long fileCrc​(String 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.
      Parameters:
      path - the path argument (not null)
      Returns:
      the result (see ChilkatObject.getLastMethodSuccess() where the description mentions failure)
    • toHex

      public String toHex​(long crc)
      Converts a 32-bit integer to a hex string.
      Parameters:
      crc - the crc argument (0 .. 4294967295)
      Returns:
      the result (never null)
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why
    • setProgressListener

      public void setProgressListener​(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; null removes it.
      Parameters:
      listener - the listener, or null
      Throws:
      IllegalStateException - if called from inside one of this object's own callbacks
    • getProgressListener

      public ProgressListener getProgressListener()
      Returns the installed listener, or null.
      Returns:
      the listener installed with setProgressListener(ProgressListener) or one of the on... methods
    • onAbortCheck

      public void onAbortCheck​(BooleanSupplier callback)
      Installs a listener whose only callback is abortCheck: called every HeartbeatMs milliseconds during a long-running method; return true to abort it. Replaces any listener installed before.
      Parameters:
      callback - the callback, or null to remove the listener
    • onPercentDone

      public void onPercentDone​(IntPredicate callback)
      Installs a listener whose only callback is percentDone: called with the completion percentage (0..100) as a long-running method progresses; return true to abort it. Replaces any listener installed before.
      Parameters:
      callback - the callback, or null to remove the listener
    • onProgressInfo

      public void onProgressInfo​(BiConsumer<String,​String> callback)
      Installs a listener whose only callback is progressInfo: called with a name/value pair of progress information as a long-running method proceeds. Replaces any listener installed before.
      Parameters:
      callback - the callback, or null to remove the listener