Sample code for 30+ languages & platforms
Xbase++

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oCompress
LOCAL oSb
LOCAL oBdCompressed
LOCAL cCompressedBase64
LOCAL oBdChunkedOut
LOCAL oSbPart
LOCAL cChunkedBase64
LOCAL oSbDecompressed

nSuccess := 0

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

oCompress := CreateObject("Chilkat.Compression")
oCompress:Algorithm := "zlib"

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

oSb := CreateObject("Chilkat.StringBuilder")
oSb:Append("The quick brown fox jumps over the lazy dog. ")
oSb:Append("This is a simple example using CompressSb.")

oBdCompressed := CreateObject("Chilkat.BinData")

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

nSuccess := oCompress:CompressSb(oSb, oBdCompressed)
IF (nSuccess == 0)
    ? oCompress:LastErrorText
    oCompress:destroy()
    oSb:destroy()
    oBdCompressed:destroy()
    RETURN
ENDIF

cCompressedBase64 := oBdCompressed:GetEncoded("base64")

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

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

oBdChunkedOut := CreateObject("Chilkat.BinData")

//  First chunk
oCompress:FirstChunk := 1
oCompress:LastChunk := 0

oSbPart := CreateObject("Chilkat.StringBuilder")
oSbPart:Append("The quick brown fox ")

nSuccess := oCompress:CompressSb(oSbPart, oBdChunkedOut)
IF (nSuccess == 0)
    ? oCompress:LastErrorText
    oCompress:destroy()
    oSb:destroy()
    oBdCompressed:destroy()
    oBdChunkedOut:destroy()
    oSbPart:destroy()
    RETURN
ENDIF

//  Middle chunk
oCompress:FirstChunk := 0
oCompress:LastChunk := 0

oSbPart:Clear()
oSbPart:Append("jumps over the lazy dog. ")

nSuccess := oCompress:CompressSb(oSbPart, oBdChunkedOut)
IF (nSuccess == 0)
    ? oCompress:LastErrorText
    oCompress:destroy()
    oSb:destroy()
    oBdCompressed:destroy()
    oBdChunkedOut:destroy()
    oSbPart:destroy()
    RETURN
ENDIF

//  Final chunk
oCompress:FirstChunk := 0
oCompress:LastChunk := 1

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

nSuccess := oCompress:CompressSb(oSbPart, oBdChunkedOut)
IF (nSuccess == 0)
    ? oCompress:LastErrorText
    oCompress:destroy()
    oSb:destroy()
    oBdCompressed:destroy()
    oBdChunkedOut:destroy()
    oSbPart:destroy()
    RETURN
ENDIF

cChunkedBase64 := oBdChunkedOut:GetEncoded("base64")

? "Chunked compressed (base64):"
? cChunkedBase64

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

oSbDecompressed := CreateObject("Chilkat.StringBuilder")

//  Decompress in a single call (entire data already assembled)
oCompress:FirstChunk := 1
oCompress:LastChunk := 1

nSuccess := oCompress:DecompressSb(oBdChunkedOut, oSbDecompressed)
IF (nSuccess == 0)
    ? oCompress:LastErrorText
    oCompress:destroy()
    oSb:destroy()
    oBdCompressed:destroy()
    oBdChunkedOut:destroy()
    oSbPart:destroy()
    oSbDecompressed:destroy()
    RETURN
ENDIF

? "Decompressed text:"
? oSbDecompressed:GetAsString()

oCompress:destroy()
oSb:destroy()
oBdCompressed:destroy()
oBdChunkedOut:destroy()
oSbPart:destroy()
oSbDecompressed:destroy()