Sample code for 30+ languages & platforms
Lua

Compressing StringBuilder Data Using CompressSb (Single Call and Chunked)

See more Compression Examples

This example demonstrates how to compress text stored in a StringBuilder using the CompressSb method in two ways:

  1. Single-call compression, where the entire input is compressed in one operation.
  2. Chunked compression, where the input is split into multiple parts and processed sequentially using the FirstChunk and LastChunk properties.

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

  • CompressSb converts text to bytes using the Charset property (UTF-8 recommended).
  • Compressed data is appended to a BinData object.
  • When both FirstChunk and LastChunk are true, compression is done in a single call.
  • For chunked processing:
    • First chunk → FirstChunk = true, LastChunk = false
    • Middle chunks → both false
    • Final chunk → LastChunk = true
  • Chunked compression is useful when processing data incrementally.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- This example assumes the Chilkat API has already been unlocked.
-- See Global Unlock Sample for sample code.

local compress = chilkat.Compression.new()
compress.algorithm = "zlib"

-- ================================================================
-- 1) Single-call compression (entire data in one call)
-- ================================================================

local sb = chilkat.StringBuilder.new()
sb:append("The quick brown fox jumps over the lazy dog. ")
sb:append("This is a simple example using CompressSb.")

local bdCompressed = chilkat.BinData.new()

-- When both FirstChunk and LastChunk are true (the defaults),
-- the entire compression happens in a single call.
compress.first_chunk = true
compress.last_chunk = true

local success = compress:compress_sb(sb, bdCompressed)
if not success then
    print(compress.last_error_text)
    return
end

local compressedBase64 = bdCompressed:get_encoded("base64")

print("Single-call compressed (base64):")
print(compressedBase64)

-- ================================================================
-- 2) Chunked compression using FirstChunk / LastChunk
-- ================================================================

local bdChunkedOut = chilkat.BinData.new()

-- First chunk
compress.first_chunk = true
compress.last_chunk = false

local sbPart = chilkat.StringBuilder.new()
sbPart:append("The quick brown fox ")

success = compress:compress_sb(sbPart, bdChunkedOut)
if not success then
    print(compress.last_error_text)
    return
end

-- Middle chunk
compress.first_chunk = false
compress.last_chunk = false

sbPart:clear()
sbPart:append("jumps over the lazy dog. ")

success = compress:compress_sb(sbPart, bdChunkedOut)
if not success then
    print(compress.last_error_text)
    return
end

-- Final chunk
compress.first_chunk = false
compress.last_chunk = true

sbPart:clear()
sbPart:append("This is a chunked CompressSb example.")

success = compress:compress_sb(sbPart, bdChunkedOut)
if not success then
    print(compress.last_error_text)
    return
end

local chunkedBase64 = bdChunkedOut:get_encoded("base64")

print("Chunked compressed (base64):")
print(chunkedBase64)

-- ================================================================
-- 3) Decompress to verify correctness
-- ================================================================

local sbDecompressed = chilkat.StringBuilder.new()

-- Decompress in a single call (entire data already assembled)
compress.first_chunk = true
compress.last_chunk = true

success = compress:decompress_sb(bdChunkedOut, sbDecompressed)
if not success then
    print(compress.last_error_text)
    return
end

print("Decompressed text:")
print(sbDecompressed:get_as_string())