Sample code for 30+ languages & platforms
PowerBuilder

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Gzip
oleobject loo_Sb
oleobject loo_Bd

li_Success = 0

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

loo_Gzip = create oleobject
li_rc = loo_Gzip.ConnectToNewObject("Chilkat.Gzip")
if li_rc < 0 then
    destroy loo_Gzip
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Sb = create oleobject
li_rc = loo_Sb.ConnectToNewObject("Chilkat.StringBuilder")

loo_Bd = create oleobject
li_rc = loo_Bd.ConnectToNewObject("Chilkat.BinData")

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

// Compress the text using UTF-8 encoding:
li_Success = loo_Gzip.CompressSb(loo_Sb,"utf-8",loo_Bd)
if li_Success = 0 then
    Write-Debug loo_Gzip.LastErrorText
    destroy loo_Gzip
    destroy loo_Sb
    destroy loo_Bd
    return
end if

// The BinData now contains the Gzip-compressed bytes.
Write-Debug "Compression successful."
Write-Debug "Compressed size (bytes): " + string(loo_Bd.NumBytes)

// (Optional) Save to a .gz file:
li_Success = loo_Bd.WriteFile("text.gz")
if li_Success = 0 then
    Write-Debug loo_Bd.LastErrorText
    destroy loo_Gzip
    destroy loo_Sb
    destroy loo_Bd
    return
end if

Write-Debug "Gzip file written to text.gz"


destroy loo_Gzip
destroy loo_Sb
destroy loo_Bd