PowerBuilder
PowerBuilder
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 PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Compress
oleobject loo_Sb
oleobject loo_BdCompressed
string ls_CompressedBase64
oleobject loo_BdChunkedOut
oleobject loo_SbPart
string ls_ChunkedBase64
oleobject loo_SbDecompressed
li_Success = 0
// This example assumes the Chilkat API has already been unlocked.
// See Global Unlock Sample for sample code.
loo_Compress = create oleobject
li_rc = loo_Compress.ConnectToNewObject("Chilkat.Compression")
if li_rc < 0 then
destroy loo_Compress
MessageBox("Error","Connecting to COM object failed")
return
end if
loo_Compress.Algorithm = "zlib"
// ================================================================
// 1) Single-call compression (entire data in one call)
// ================================================================
loo_Sb = create oleobject
li_rc = loo_Sb.ConnectToNewObject("Chilkat.StringBuilder")
loo_Sb.Append("The quick brown fox jumps over the lazy dog. ")
loo_Sb.Append("This is a simple example using CompressSb.")
loo_BdCompressed = create oleobject
li_rc = loo_BdCompressed.ConnectToNewObject("Chilkat.BinData")
// When both FirstChunk and LastChunk are true (the defaults),
// the entire compression happens in a single call.
loo_Compress.FirstChunk = 1
loo_Compress.LastChunk = 1
li_Success = loo_Compress.CompressSb(loo_Sb,loo_BdCompressed)
if li_Success = 0 then
Write-Debug loo_Compress.LastErrorText
destroy loo_Compress
destroy loo_Sb
destroy loo_BdCompressed
return
end if
ls_CompressedBase64 = loo_BdCompressed.GetEncoded("base64")
Write-Debug "Single-call compressed (base64):"
Write-Debug ls_CompressedBase64
// ================================================================
// 2) Chunked compression using FirstChunk / LastChunk
// ================================================================
loo_BdChunkedOut = create oleobject
li_rc = loo_BdChunkedOut.ConnectToNewObject("Chilkat.BinData")
// First chunk
loo_Compress.FirstChunk = 1
loo_Compress.LastChunk = 0
loo_SbPart = create oleobject
li_rc = loo_SbPart.ConnectToNewObject("Chilkat.StringBuilder")
loo_SbPart.Append("The quick brown fox ")
li_Success = loo_Compress.CompressSb(loo_SbPart,loo_BdChunkedOut)
if li_Success = 0 then
Write-Debug loo_Compress.LastErrorText
destroy loo_Compress
destroy loo_Sb
destroy loo_BdCompressed
destroy loo_BdChunkedOut
destroy loo_SbPart
return
end if
// Middle chunk
loo_Compress.FirstChunk = 0
loo_Compress.LastChunk = 0
loo_SbPart.Clear()
loo_SbPart.Append("jumps over the lazy dog. ")
li_Success = loo_Compress.CompressSb(loo_SbPart,loo_BdChunkedOut)
if li_Success = 0 then
Write-Debug loo_Compress.LastErrorText
destroy loo_Compress
destroy loo_Sb
destroy loo_BdCompressed
destroy loo_BdChunkedOut
destroy loo_SbPart
return
end if
// Final chunk
loo_Compress.FirstChunk = 0
loo_Compress.LastChunk = 1
loo_SbPart.Clear()
loo_SbPart.Append("This is a chunked CompressSb example.")
li_Success = loo_Compress.CompressSb(loo_SbPart,loo_BdChunkedOut)
if li_Success = 0 then
Write-Debug loo_Compress.LastErrorText
destroy loo_Compress
destroy loo_Sb
destroy loo_BdCompressed
destroy loo_BdChunkedOut
destroy loo_SbPart
return
end if
ls_ChunkedBase64 = loo_BdChunkedOut.GetEncoded("base64")
Write-Debug "Chunked compressed (base64):"
Write-Debug ls_ChunkedBase64
// ================================================================
// 3) Decompress to verify correctness
// ================================================================
loo_SbDecompressed = create oleobject
li_rc = loo_SbDecompressed.ConnectToNewObject("Chilkat.StringBuilder")
// Decompress in a single call (entire data already assembled)
loo_Compress.FirstChunk = 1
loo_Compress.LastChunk = 1
li_Success = loo_Compress.DecompressSb(loo_BdChunkedOut,loo_SbDecompressed)
if li_Success = 0 then
Write-Debug loo_Compress.LastErrorText
destroy loo_Compress
destroy loo_Sb
destroy loo_BdCompressed
destroy loo_BdChunkedOut
destroy loo_SbPart
destroy loo_SbDecompressed
return
end if
Write-Debug "Decompressed text:"
Write-Debug loo_SbDecompressed.GetAsString()
destroy loo_Compress
destroy loo_Sb
destroy loo_BdCompressed
destroy loo_BdChunkedOut
destroy loo_SbPart
destroy loo_SbDecompressed