AutoIt
AutoIt
Compressing StringBuilder Data Using CompressSb (Single Call and Chunked)
See more Compression Examples
This example demonstrates how to compress text stored in aStringBuilder using the CompressSb method in two ways:
- Single-call compression, where the entire input is compressed in one operation.
- Chunked compression, where the input is split into multiple parts and processed sequentially using the
FirstChunkandLastChunkproperties.
The example shows how compressed output is appended to a BinData object, and how the result can be verified by decompressing back to the original text. This is useful for both simple use cases and scenarios where data is processed incrementally (such as streaming or large inputs).
Key Points
CompressSbconverts text to bytes using theCharsetproperty (UTF-8 recommended).- Compressed data is appended to a
BinDataobject. - When both
FirstChunkandLastChunkaretrue, compression is done in a single call. - For chunked processing:
- First chunk →
FirstChunk = true,LastChunk = false - Middle chunks → both
false - Final chunk →
LastChunk = true
- First chunk →
- Chunked compression is useful when processing data incrementally.
Chilkat AutoIt Downloads
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"
; ================================================================
; 1) Single-call compression (entire data in one call)
; ================================================================
$oSb = ObjCreate("Chilkat.StringBuilder")
$oSb.Append("The quick brown fox jumps over the lazy dog. ")
$oSb.Append("This is a simple example using CompressSb.")
$oBdCompressed = ObjCreate("Chilkat.BinData")
; When both FirstChunk and LastChunk are true (the defaults),
; the entire compression happens in a single call.
$oCompress.FirstChunk = True
$oCompress.LastChunk = True
$bSuccess = $oCompress.CompressSb($oSb,$oBdCompressed)
If ($bSuccess = False) Then
ConsoleWrite($oCompress.LastErrorText & @CRLF)
Exit
EndIf
Local $sCompressedBase64 = $oBdCompressed.GetEncoded("base64")
ConsoleWrite("Single-call compressed (base64):" & @CRLF)
ConsoleWrite($sCompressedBase64 & @CRLF)
; ================================================================
; 2) Chunked compression using FirstChunk / LastChunk
; ================================================================
$oBdChunkedOut = ObjCreate("Chilkat.BinData")
; First chunk
$oCompress.FirstChunk = True
$oCompress.LastChunk = False
$oSbPart = ObjCreate("Chilkat.StringBuilder")
$oSbPart.Append("The quick brown fox ")
$bSuccess = $oCompress.CompressSb($oSbPart,$oBdChunkedOut)
If ($bSuccess = False) Then
ConsoleWrite($oCompress.LastErrorText & @CRLF)
Exit
EndIf
; Middle chunk
$oCompress.FirstChunk = False
$oCompress.LastChunk = False
$oSbPart.Clear
$oSbPart.Append("jumps over the lazy dog. ")
$bSuccess = $oCompress.CompressSb($oSbPart,$oBdChunkedOut)
If ($bSuccess = False) Then
ConsoleWrite($oCompress.LastErrorText & @CRLF)
Exit
EndIf
; Final chunk
$oCompress.FirstChunk = False
$oCompress.LastChunk = True
$oSbPart.Clear
$oSbPart.Append("This is a chunked CompressSb example.")
$bSuccess = $oCompress.CompressSb($oSbPart,$oBdChunkedOut)
If ($bSuccess = False) Then
ConsoleWrite($oCompress.LastErrorText & @CRLF)
Exit
EndIf
Local $sChunkedBase64 = $oBdChunkedOut.GetEncoded("base64")
ConsoleWrite("Chunked compressed (base64):" & @CRLF)
ConsoleWrite($sChunkedBase64 & @CRLF)
; ================================================================
; 3) Decompress to verify correctness
; ================================================================
$oSbDecompressed = ObjCreate("Chilkat.StringBuilder")
; Decompress in a single call (entire data already assembled)
$oCompress.FirstChunk = True
$oCompress.LastChunk = True
$bSuccess = $oCompress.DecompressSb($oBdChunkedOut,$oSbDecompressed)
If ($bSuccess = False) Then
ConsoleWrite($oCompress.LastErrorText & @CRLF)
Exit
EndIf
ConsoleWrite("Decompressed text:" & @CRLF)
ConsoleWrite($oSbDecompressed.GetAsString() & @CRLF)