Sample code for 30+ languages & platforms
Visual Basic 6.0

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 Basic 6.0 Downloads

Visual Basic 6.0
Dim success As Long
success = 0

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

Dim gzip As New ChilkatGzip
Dim sb As New ChilkatStringBuilder
Dim bd As New ChilkatBinData

' Add some text to the StringBuilder:
success = sb.Append("The quick brown fox jumps over the lazy dog.")

' Compress the text using UTF-8 encoding:
success = gzip.CompressSb(sb,"utf-8",bd)
If (success = 0) Then
    Debug.Print gzip.LastErrorText
    Exit Sub
End If

' The BinData now contains the Gzip-compressed bytes.
Debug.Print "Compression successful."
Debug.Print "Compressed size (bytes): " & bd.NumBytes

' (Optional) Save to a .gz file:
success = bd.WriteFile("text.gz")
If (success = 0) Then
    Debug.Print bd.LastErrorText
    Exit Sub
End If

Debug.Print "Gzip file written to text.gz"