Visual FoxPro
Visual FoxPro
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 Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loCompress
LOCAL loSb
LOCAL loBdCompressed
LOCAL lcCompressedBase64
LOCAL loBdChunkedOut
LOCAL loSbPart
LOCAL lcChunkedBase64
LOCAL loSbDecompressed
lnSuccess = 0
* This example assumes the Chilkat API has already been unlocked.
* See Global Unlock Sample for sample code.
loCompress = CreateObject('Chilkat.Compression')
loCompress.Algorithm = "zlib"
* ================================================================
* 1) Single-call compression (entire data in one call)
* ================================================================
loSb = CreateObject('Chilkat.StringBuilder')
loSb.Append("The quick brown fox jumps over the lazy dog. ")
loSb.Append("This is a simple example using CompressSb.")
loBdCompressed = CreateObject('Chilkat.BinData')
* When both FirstChunk and LastChunk are true (the defaults),
* the entire compression happens in a single call.
loCompress.FirstChunk = 1
loCompress.LastChunk = 1
lnSuccess = loCompress.CompressSb(loSb,loBdCompressed)
IF (lnSuccess = 0) THEN
? loCompress.LastErrorText
RELEASE loCompress
RELEASE loSb
RELEASE loBdCompressed
CANCEL
ENDIF
lcCompressedBase64 = loBdCompressed.GetEncoded("base64")
? "Single-call compressed (base64):"
? lcCompressedBase64
* ================================================================
* 2) Chunked compression using FirstChunk / LastChunk
* ================================================================
loBdChunkedOut = CreateObject('Chilkat.BinData')
* First chunk
loCompress.FirstChunk = 1
loCompress.LastChunk = 0
loSbPart = CreateObject('Chilkat.StringBuilder')
loSbPart.Append("The quick brown fox ")
lnSuccess = loCompress.CompressSb(loSbPart,loBdChunkedOut)
IF (lnSuccess = 0) THEN
? loCompress.LastErrorText
RELEASE loCompress
RELEASE loSb
RELEASE loBdCompressed
RELEASE loBdChunkedOut
RELEASE loSbPart
CANCEL
ENDIF
* Middle chunk
loCompress.FirstChunk = 0
loCompress.LastChunk = 0
loSbPart.Clear()
loSbPart.Append("jumps over the lazy dog. ")
lnSuccess = loCompress.CompressSb(loSbPart,loBdChunkedOut)
IF (lnSuccess = 0) THEN
? loCompress.LastErrorText
RELEASE loCompress
RELEASE loSb
RELEASE loBdCompressed
RELEASE loBdChunkedOut
RELEASE loSbPart
CANCEL
ENDIF
* Final chunk
loCompress.FirstChunk = 0
loCompress.LastChunk = 1
loSbPart.Clear()
loSbPart.Append("This is a chunked CompressSb example.")
lnSuccess = loCompress.CompressSb(loSbPart,loBdChunkedOut)
IF (lnSuccess = 0) THEN
? loCompress.LastErrorText
RELEASE loCompress
RELEASE loSb
RELEASE loBdCompressed
RELEASE loBdChunkedOut
RELEASE loSbPart
CANCEL
ENDIF
lcChunkedBase64 = loBdChunkedOut.GetEncoded("base64")
? "Chunked compressed (base64):"
? lcChunkedBase64
* ================================================================
* 3) Decompress to verify correctness
* ================================================================
loSbDecompressed = CreateObject('Chilkat.StringBuilder')
* Decompress in a single call (entire data already assembled)
loCompress.FirstChunk = 1
loCompress.LastChunk = 1
lnSuccess = loCompress.DecompressSb(loBdChunkedOut,loSbDecompressed)
IF (lnSuccess = 0) THEN
? loCompress.LastErrorText
RELEASE loCompress
RELEASE loSb
RELEASE loBdCompressed
RELEASE loBdChunkedOut
RELEASE loSbPart
RELEASE loSbDecompressed
CANCEL
ENDIF
? "Decompressed text:"
? loSbDecompressed.GetAsString()
RELEASE loCompress
RELEASE loSb
RELEASE loBdCompressed
RELEASE loBdChunkedOut
RELEASE loSbPart
RELEASE loSbDecompressed