VBScript
VBScript
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 VBScript Downloads
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)
success = 0
' This example assumes the Chilkat API has already been unlocked.
' See Global Unlock Sample for sample code.
set compress = CreateObject("Chilkat.Compression")
compress.Algorithm = "zlib"
' ================================================================
' 1) Single-call compression (entire data in one call)
' ================================================================
set sb = CreateObject("Chilkat.StringBuilder")
success = sb.Append("The quick brown fox jumps over the lazy dog. ")
success = sb.Append("This is a simple example using CompressSb.")
set bdCompressed = CreateObject("Chilkat.BinData")
' When both FirstChunk and LastChunk are true (the defaults),
' the entire compression happens in a single call.
compress.FirstChunk = 1
compress.LastChunk = 1
success = compress.CompressSb(sb,bdCompressed)
If (success = 0) Then
outFile.WriteLine(compress.LastErrorText)
WScript.Quit
End If
compressedBase64 = bdCompressed.GetEncoded("base64")
outFile.WriteLine("Single-call compressed (base64):")
outFile.WriteLine(compressedBase64)
' ================================================================
' 2) Chunked compression using FirstChunk / LastChunk
' ================================================================
set bdChunkedOut = CreateObject("Chilkat.BinData")
' First chunk
compress.FirstChunk = 1
compress.LastChunk = 0
set sbPart = CreateObject("Chilkat.StringBuilder")
success = sbPart.Append("The quick brown fox ")
success = compress.CompressSb(sbPart,bdChunkedOut)
If (success = 0) Then
outFile.WriteLine(compress.LastErrorText)
WScript.Quit
End If
' Middle chunk
compress.FirstChunk = 0
compress.LastChunk = 0
sbPart.Clear
success = sbPart.Append("jumps over the lazy dog. ")
success = compress.CompressSb(sbPart,bdChunkedOut)
If (success = 0) Then
outFile.WriteLine(compress.LastErrorText)
WScript.Quit
End If
' Final chunk
compress.FirstChunk = 0
compress.LastChunk = 1
sbPart.Clear
success = sbPart.Append("This is a chunked CompressSb example.")
success = compress.CompressSb(sbPart,bdChunkedOut)
If (success = 0) Then
outFile.WriteLine(compress.LastErrorText)
WScript.Quit
End If
chunkedBase64 = bdChunkedOut.GetEncoded("base64")
outFile.WriteLine("Chunked compressed (base64):")
outFile.WriteLine(chunkedBase64)
' ================================================================
' 3) Decompress to verify correctness
' ================================================================
set sbDecompressed = CreateObject("Chilkat.StringBuilder")
' Decompress in a single call (entire data already assembled)
compress.FirstChunk = 1
compress.LastChunk = 1
success = compress.DecompressSb(bdChunkedOut,sbDecompressed)
If (success = 0) Then
outFile.WriteLine(compress.LastErrorText)
WScript.Quit
End If
outFile.WriteLine("Decompressed text:")
outFile.WriteLine(sbDecompressed.GetAsString())
outFile.Close