Sample code for 30+ languages & platforms
Tcl

Compressing StringBuilder Data Using CompressSb (Single Call and Chunked)

See more Compression Examples

This example demonstrates how to compress text stored in a StringBuilder using the CompressSb method in two ways:

  1. Single-call compression, where the entire input is compressed in one operation.
  2. Chunked compression, where the input is split into multiple parts and processed sequentially using the FirstChunk and LastChunk properties.

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

  • CompressSb converts text to bytes using the Charset property (UTF-8 recommended).
  • Compressed data is appended to a BinData object.
  • When both FirstChunk and LastChunk are true, compression is done in a single call.
  • For chunked processing:
    • First chunk → FirstChunk = true, LastChunk = false
    • Middle chunks → both false
    • Final chunk → LastChunk = true
  • Chunked compression is useful when processing data incrementally.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

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

set compress [new_CkCompression]

CkCompression_put_Algorithm $compress "zlib"

# ================================================================
# 1) Single-call compression (entire data in one call)
# ================================================================

set sb [new_CkStringBuilder]

CkStringBuilder_Append $sb "The quick brown fox jumps over the lazy dog. "
CkStringBuilder_Append $sb "This is a simple example using CompressSb."

set bdCompressed [new_CkBinData]

# When both FirstChunk and LastChunk are true (the defaults),
# the entire compression happens in a single call.
CkCompression_put_FirstChunk $compress 1
CkCompression_put_LastChunk $compress 1

set success [CkCompression_CompressSb $compress $sb $bdCompressed]
if {$success == 0} then {
    puts [CkCompression_lastErrorText $compress]
    delete_CkCompression $compress
    delete_CkStringBuilder $sb
    delete_CkBinData $bdCompressed
    exit
}

set compressedBase64 [CkBinData_getEncoded $bdCompressed "base64"]

puts "Single-call compressed (base64):"
puts "$compressedBase64"

# ================================================================
# 2) Chunked compression using FirstChunk / LastChunk
# ================================================================

set bdChunkedOut [new_CkBinData]

# First chunk
CkCompression_put_FirstChunk $compress 1
CkCompression_put_LastChunk $compress 0

set sbPart [new_CkStringBuilder]

CkStringBuilder_Append $sbPart "The quick brown fox "

set success [CkCompression_CompressSb $compress $sbPart $bdChunkedOut]
if {$success == 0} then {
    puts [CkCompression_lastErrorText $compress]
    delete_CkCompression $compress
    delete_CkStringBuilder $sb
    delete_CkBinData $bdCompressed
    delete_CkBinData $bdChunkedOut
    delete_CkStringBuilder $sbPart
    exit
}

# Middle chunk
CkCompression_put_FirstChunk $compress 0
CkCompression_put_LastChunk $compress 0

CkStringBuilder_Clear $sbPart
CkStringBuilder_Append $sbPart "jumps over the lazy dog. "

set success [CkCompression_CompressSb $compress $sbPart $bdChunkedOut]
if {$success == 0} then {
    puts [CkCompression_lastErrorText $compress]
    delete_CkCompression $compress
    delete_CkStringBuilder $sb
    delete_CkBinData $bdCompressed
    delete_CkBinData $bdChunkedOut
    delete_CkStringBuilder $sbPart
    exit
}

# Final chunk
CkCompression_put_FirstChunk $compress 0
CkCompression_put_LastChunk $compress 1

CkStringBuilder_Clear $sbPart
CkStringBuilder_Append $sbPart "This is a chunked CompressSb example."

set success [CkCompression_CompressSb $compress $sbPart $bdChunkedOut]
if {$success == 0} then {
    puts [CkCompression_lastErrorText $compress]
    delete_CkCompression $compress
    delete_CkStringBuilder $sb
    delete_CkBinData $bdCompressed
    delete_CkBinData $bdChunkedOut
    delete_CkStringBuilder $sbPart
    exit
}

set chunkedBase64 [CkBinData_getEncoded $bdChunkedOut "base64"]

puts "Chunked compressed (base64):"
puts "$chunkedBase64"

# ================================================================
# 3) Decompress to verify correctness
# ================================================================

set sbDecompressed [new_CkStringBuilder]

# Decompress in a single call (entire data already assembled)
CkCompression_put_FirstChunk $compress 1
CkCompression_put_LastChunk $compress 1

set success [CkCompression_DecompressSb $compress $bdChunkedOut $sbDecompressed]
if {$success == 0} then {
    puts [CkCompression_lastErrorText $compress]
    delete_CkCompression $compress
    delete_CkStringBuilder $sb
    delete_CkBinData $bdCompressed
    delete_CkBinData $bdChunkedOut
    delete_CkStringBuilder $sbPart
    delete_CkStringBuilder $sbDecompressed
    exit
}

puts "Decompressed text:"
puts [CkStringBuilder_getAsString $sbDecompressed]

delete_CkCompression $compress
delete_CkStringBuilder $sb
delete_CkBinData $bdCompressed
delete_CkBinData $bdChunkedOut
delete_CkStringBuilder $sbPart
delete_CkStringBuilder $sbDecompressed