PowerBuilder
PowerBuilder
Chunked Compression Using CompressBd2 with FirstChunk and LastChunk
See more Compression Examples
This example demonstrates how to compress data in multiple segments using theCompressBd2 method together with the FirstChunk and LastChunk properties. Instead of compressing all data in a single call, the input is divided into smaller chunks and processed sequentially, which is useful when handling streaming data or large inputs.
The example shows how to correctly mark the first, middle, and final chunks so that the compression stream is properly initialized and finalized. The compressed output is accumulated in a separate BinData object without modifying the input chunks. Finally, the example decompresses the combined result to verify that the original data is restored correctly.
This approach is ideal for scenarios where data is received incrementally, such as reading from a network stream, processing large files in parts, or handling real-time data feeds.
Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Compress
oleobject loo_BdOut
string ls_Part1
string ls_Part2
string ls_Part3
oleobject loo_BdIn
string ls_CompressedBase64
oleobject loo_BdDecompressed
string ls_ResultText
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"
// This will accumulate the compressed output.
loo_BdOut = create oleobject
li_rc = loo_BdOut.ConnectToNewObject("Chilkat.BinData")
// ------------------------------------------------------------------
// Simulate input arriving in chunks.
// ------------------------------------------------------------------
ls_Part1 = "The quick brown fox "
ls_Part2 = "jumps over the lazy dog. "
ls_Part3 = "This text is split into chunks."
// ------------------------------------------------------------------
// Compress the first chunk
// ------------------------------------------------------------------
loo_Compress.FirstChunk = 1
loo_Compress.LastChunk = 0
loo_BdIn = create oleobject
li_rc = loo_BdIn.ConnectToNewObject("Chilkat.BinData")
loo_BdIn.AppendString(ls_Part1,"utf-8")
li_Success = loo_Compress.CompressBd2(loo_BdIn,loo_BdOut)
if li_Success = 0 then
Write-Debug loo_Compress.LastErrorText
destroy loo_Compress
destroy loo_BdOut
destroy loo_BdIn
return
end if
// ------------------------------------------------------------------
// Compress a middle chunk
// ------------------------------------------------------------------
loo_Compress.FirstChunk = 0
loo_Compress.LastChunk = 0
loo_BdIn.Clear()
loo_BdIn.AppendString(ls_Part2,"utf-8")
li_Success = loo_Compress.CompressBd2(loo_BdIn,loo_BdOut)
if li_Success = 0 then
Write-Debug loo_Compress.LastErrorText
destroy loo_Compress
destroy loo_BdOut
destroy loo_BdIn
return
end if
// ------------------------------------------------------------------
// Compress the final chunk
// ------------------------------------------------------------------
loo_Compress.FirstChunk = 0
loo_Compress.LastChunk = 1
loo_BdIn.Clear()
loo_BdIn.AppendString(ls_Part3,"utf-8")
li_Success = loo_Compress.CompressBd2(loo_BdIn,loo_BdOut)
if li_Success = 0 then
Write-Debug loo_Compress.LastErrorText
destroy loo_Compress
destroy loo_BdOut
destroy loo_BdIn
return
end if
// Get the final compressed result as base64 for display
ls_CompressedBase64 = loo_BdOut.GetEncoded("base64")
Write-Debug "Compressed data (base64):"
Write-Debug ls_CompressedBase64
// ------------------------------------------------------------------
// Decompress to verify correctness
// ------------------------------------------------------------------
loo_BdDecompressed = create oleobject
li_rc = loo_BdDecompressed.ConnectToNewObject("Chilkat.BinData")
loo_Compress.FirstChunk = 1
loo_Compress.LastChunk = 1
li_Success = loo_Compress.DecompressBd2(loo_BdOut,loo_BdDecompressed)
if li_Success = 0 then
Write-Debug loo_Compress.LastErrorText
destroy loo_Compress
destroy loo_BdOut
destroy loo_BdIn
destroy loo_BdDecompressed
return
end if
// Convert decompressed bytes back to a string
ls_ResultText = loo_BdDecompressed.GetString("utf-8")
Write-Debug "Decompressed text:"
Write-Debug ls_ResultText
destroy loo_Compress
destroy loo_BdOut
destroy loo_BdIn
destroy loo_BdDecompressed