Sample code for 30+ languages & platforms
Lianja

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 Lianja Downloads

Lianja
llSuccess = .F.

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

loCompress = createobject("CkCompression")
loCompress.Algorithm = "zlib"

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

loSb = createobject("CkStringBuilder")
loSb.Append("The quick brown fox jumps over the lazy dog. ")
loSb.Append("This is a simple example using CompressSb.")

loBdCompressed = createobject("CkBinData")

// When both FirstChunk and LastChunk are true (the defaults),
// the entire compression happens in a single call.
loCompress.FirstChunk = .T.
loCompress.LastChunk = .T.

llSuccess = loCompress.CompressSb(loSb,loBdCompressed)
if (llSuccess = .F.) then
    ? loCompress.LastErrorText
    release loCompress
    release loSb
    release loBdCompressed
    return
endif

lcCompressedBase64 = loBdCompressed.GetEncoded("base64")

? "Single-call compressed (base64):"
? lcCompressedBase64

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

loBdChunkedOut = createobject("CkBinData")

// First chunk
loCompress.FirstChunk = .T.
loCompress.LastChunk = .F.

loSbPart = createobject("CkStringBuilder")
loSbPart.Append("The quick brown fox ")

llSuccess = loCompress.CompressSb(loSbPart,loBdChunkedOut)
if (llSuccess = .F.) then
    ? loCompress.LastErrorText
    release loCompress
    release loSb
    release loBdCompressed
    release loBdChunkedOut
    release loSbPart
    return
endif

// Middle chunk
loCompress.FirstChunk = .F.
loCompress.LastChunk = .F.

loSbPart.Clear()
loSbPart.Append("jumps over the lazy dog. ")

llSuccess = loCompress.CompressSb(loSbPart,loBdChunkedOut)
if (llSuccess = .F.) then
    ? loCompress.LastErrorText
    release loCompress
    release loSb
    release loBdCompressed
    release loBdChunkedOut
    release loSbPart
    return
endif

// Final chunk
loCompress.FirstChunk = .F.
loCompress.LastChunk = .T.

loSbPart.Clear()
loSbPart.Append("This is a chunked CompressSb example.")

llSuccess = loCompress.CompressSb(loSbPart,loBdChunkedOut)
if (llSuccess = .F.) then
    ? loCompress.LastErrorText
    release loCompress
    release loSb
    release loBdCompressed
    release loBdChunkedOut
    release loSbPart
    return
endif

lcChunkedBase64 = loBdChunkedOut.GetEncoded("base64")

? "Chunked compressed (base64):"
? lcChunkedBase64

// ================================================================
// 3) Decompress to verify correctness
// ================================================================

loSbDecompressed = createobject("CkStringBuilder")

// Decompress in a single call (entire data already assembled)
loCompress.FirstChunk = .T.
loCompress.LastChunk = .T.

llSuccess = loCompress.DecompressSb(loBdChunkedOut,loSbDecompressed)
if (llSuccess = .F.) then
    ? loCompress.LastErrorText
    release loCompress
    release loSb
    release loBdCompressed
    release loBdChunkedOut
    release loSbPart
    release loSbDecompressed
    return
endif

? "Decompressed text:"
? loSbDecompressed.GetAsString()


release loCompress
release loSb
release loBdCompressed
release loBdChunkedOut
release loSbPart
release loSbDecompressed