Lua
Lua
Chunked Compression Using CompressBd2 with FirstChunk and LastChunk
See more Compression Examples
This example demonstrates how to compress data in multiple segments using theCompressBd2 method together with the FirstChunk and LastChunk properties. Instead of compressing all data in a single call, the input is divided into smaller chunks and processed sequentially, which is useful when handling streaming data or large inputs.
The example shows how to correctly mark the first, middle, and final chunks so that the compression stream is properly initialized and finalized. The compressed output is accumulated in a separate BinData object without modifying the input chunks. Finally, the example decompresses the combined result to verify that the original data is restored correctly.
This approach is ideal for scenarios where data is received incrementally, such as reading from a network stream, processing large files in parts, or handling real-time data feeds.
Chilkat Lua Downloads
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"
-- This will accumulate the compressed output.
local bdOut = chilkat.BinData.new()
-- ------------------------------------------------------------------
-- Simulate input arriving in chunks.
-- ------------------------------------------------------------------
local part1 = "The quick brown fox "
local part2 = "jumps over the lazy dog. "
local part3 = "This text is split into chunks."
-- ------------------------------------------------------------------
-- Compress the first chunk
-- ------------------------------------------------------------------
compress.first_chunk = true
compress.last_chunk = false
local bdIn = chilkat.BinData.new()
bdIn:append_string(part1, "utf-8")
local success = compress:compress_bd2(bdIn, bdOut)
if not success then
print(compress.last_error_text)
return
end
-- ------------------------------------------------------------------
-- Compress a middle chunk
-- ------------------------------------------------------------------
compress.first_chunk = false
compress.last_chunk = false
bdIn:clear()
bdIn:append_string(part2, "utf-8")
success = compress:compress_bd2(bdIn, bdOut)
if not success then
print(compress.last_error_text)
return
end
-- ------------------------------------------------------------------
-- Compress the final chunk
-- ------------------------------------------------------------------
compress.first_chunk = false
compress.last_chunk = true
bdIn:clear()
bdIn:append_string(part3, "utf-8")
success = compress:compress_bd2(bdIn, bdOut)
if not success then
print(compress.last_error_text)
return
end
-- Get the final compressed result as base64 for display
local compressedBase64 = bdOut:get_encoded("base64")
print("Compressed data (base64):")
print(compressedBase64)
-- ------------------------------------------------------------------
-- Decompress to verify correctness
-- ------------------------------------------------------------------
local bdDecompressed = chilkat.BinData.new()
compress.first_chunk = true
compress.last_chunk = true
success = compress:decompress_bd2(bdOut, bdDecompressed)
if not success then
print(compress.last_error_text)
return
end
-- Convert decompressed bytes back to a string
local resultText = bdDecompressed:get_string("utf-8")
print("Decompressed text:")
print(resultText)