Sample code for 30+ languages & platforms
Visual FoxPro

Compress Text from StringBuilder to Gzip (BinData Output)

See more Gzip Examples

This example demonstrates how to use the CompressSb method to compress text stored in a StringBuilder into Gzip format.

The text is first converted to its byte representation using the specified character set (in this case, UTF-8). These bytes are then compressed, and the resulting Gzip data is written to a BinData object in memory.

This approach is useful when working with dynamically generated text that you want to compress without first writing it to a file. The example also shows how the compressed data can optionally be saved to a .gz file.

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loGzip
LOCAL loSb
LOCAL loBd

lnSuccess = 0

* This example demonstrates how to compress text contained in a StringBuilder
* into Gzip format, storing the compressed result in a BinData object.

loGzip = CreateObject('Chilkat.Gzip')
loSb = CreateObject('Chilkat.StringBuilder')
loBd = CreateObject('Chilkat.BinData')

* Add some text to the StringBuilder:
loSb.Append("The quick brown fox jumps over the lazy dog.")

* Compress the text using UTF-8 encoding:
lnSuccess = loGzip.CompressSb(loSb,"utf-8",loBd)
IF (lnSuccess = 0) THEN
    ? loGzip.LastErrorText
    RELEASE loGzip
    RELEASE loSb
    RELEASE loBd
    CANCEL
ENDIF

* The BinData now contains the Gzip-compressed bytes.
? "Compression successful."
? "Compressed size (bytes): " + STR(loBd.NumBytes)

* (Optional) Save to a .gz file:
lnSuccess = loBd.WriteFile("text.gz")
IF (lnSuccess = 0) THEN
    ? loBd.LastErrorText
    RELEASE loGzip
    RELEASE loSb
    RELEASE loBd
    CANCEL
ENDIF

? "Gzip file written to text.gz"

RELEASE loGzip
RELEASE loSb
RELEASE loBd