Sample code for 30+ languages & platforms
AutoIt

Chunked Compression Using CompressBd2 with FirstChunk and LastChunk

See more Compression Examples

This example demonstrates how to compress data in multiple segments using the CompressBd2 method together with the FirstChunk and LastChunk properties. Instead of compressing all data in a single call, the input is divided into smaller chunks and processed sequentially, which is useful when handling streaming data or large inputs.

The example shows how to correctly mark the first, middle, and final chunks so that the compression stream is properly initialized and finalized. The compressed output is accumulated in a separate BinData object without modifying the input chunks. Finally, the example decompresses the combined result to verify that the original data is restored correctly.

This approach is ideal for scenarios where data is received incrementally, such as reading from a network stream, processing large files in parts, or handling real-time data feeds.

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

; This example assumes the Chilkat API has already been unlocked.
; See Global Unlock Sample for sample code.

$oCompress = ObjCreate("Chilkat.Compression")
$oCompress.Algorithm = "zlib"

; This will accumulate the compressed output.
$oBdOut = ObjCreate("Chilkat.BinData")

; ------------------------------------------------------------------
; Simulate input arriving in chunks.
; ------------------------------------------------------------------

Local $sPart1 = "The quick brown fox "
Local $sPart2 = "jumps over the lazy dog. "
Local $sPart3 = "This text is split into chunks."

; ------------------------------------------------------------------
; Compress the first chunk
; ------------------------------------------------------------------

$oCompress.FirstChunk = True
$oCompress.LastChunk = False

$oBdIn = ObjCreate("Chilkat.BinData")
$oBdIn.AppendString($sPart1,"utf-8")

$bSuccess = $oCompress.CompressBd2($oBdIn,$oBdOut)
If ($bSuccess = False) Then
    ConsoleWrite($oCompress.LastErrorText & @CRLF)
    Exit
EndIf

; ------------------------------------------------------------------
; Compress a middle chunk
; ------------------------------------------------------------------

$oCompress.FirstChunk = False
$oCompress.LastChunk = False

$oBdIn.Clear()
$oBdIn.AppendString($sPart2,"utf-8")

$bSuccess = $oCompress.CompressBd2($oBdIn,$oBdOut)
If ($bSuccess = False) Then
    ConsoleWrite($oCompress.LastErrorText & @CRLF)
    Exit
EndIf

; ------------------------------------------------------------------
; Compress the final chunk
; ------------------------------------------------------------------

$oCompress.FirstChunk = False
$oCompress.LastChunk = True

$oBdIn.Clear()
$oBdIn.AppendString($sPart3,"utf-8")

$bSuccess = $oCompress.CompressBd2($oBdIn,$oBdOut)
If ($bSuccess = False) Then
    ConsoleWrite($oCompress.LastErrorText & @CRLF)
    Exit
EndIf

; Get the final compressed result as base64 for display
Local $sCompressedBase64 = $oBdOut.GetEncoded("base64")

ConsoleWrite("Compressed data (base64):" & @CRLF)
ConsoleWrite($sCompressedBase64 & @CRLF)

; ------------------------------------------------------------------
; Decompress to verify correctness
; ------------------------------------------------------------------

$oBdDecompressed = ObjCreate("Chilkat.BinData")

$oCompress.FirstChunk = True
$oCompress.LastChunk = True

$bSuccess = $oCompress.DecompressBd2($oBdOut,$oBdDecompressed)
If ($bSuccess = False) Then
    ConsoleWrite($oCompress.LastErrorText & @CRLF)
    Exit
EndIf

; Convert decompressed bytes back to a string
Local $sResultText = $oBdDecompressed.GetString("utf-8")

ConsoleWrite("Decompressed text:" & @CRLF)
ConsoleWrite($sResultText & @CRLF)