DataFlex
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoGzip
Variant vSb
Handle hoSb
Variant vBd
Handle hoBd
String sTemp1
Integer iTemp1
Move False To iSuccess
// This example demonstrates how to compress text contained in a StringBuilder
// into Gzip format, storing the compressed result in a BinData object.
Get Create (RefClass(cComChilkatGzip)) To hoGzip
If (Not(IsComObjectCreated(hoGzip))) Begin
Send CreateComObject of hoGzip
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoSb
If (Not(IsComObjectCreated(hoSb))) Begin
Send CreateComObject of hoSb
End
Get Create (RefClass(cComChilkatBinData)) To hoBd
If (Not(IsComObjectCreated(hoBd))) Begin
Send CreateComObject of hoBd
End
// Add some text to the StringBuilder:
Get ComAppend Of hoSb "The quick brown fox jumps over the lazy dog." To iSuccess
// Compress the text using UTF-8 encoding:
Get pvComObject of hoSb to vSb
Get pvComObject of hoBd to vBd
Get ComCompressSb Of hoGzip vSb "utf-8" vBd To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoGzip To sTemp1
Showln sTemp1
Procedure_Return
End
// The BinData now contains the Gzip-compressed bytes.
Showln "Compression successful."
Get ComNumBytes Of hoBd To iTemp1
Showln "Compressed size (bytes): " iTemp1
// (Optional) Save to a .gz file:
Get ComWriteFile Of hoBd "text.gz" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoBd To sTemp1
Showln sTemp1
Procedure_Return
End
Showln "Gzip file written to text.gz"
End_Procedure