Sample code for 30+ languages & platforms
Lua Requires Chilkat v11.0.0+

Compress String Feed to Base64

See more Compression Examples

This example receives incoming text data in chunks, compresses as a stream, and accumulates the compressed data in base64.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

local success = false

-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.

local sbCompressedBase64 = chilkat.StringBuilder.new()

local compress = chilkat.Compression.new()
compress.algorithm = "deflate"
compress.charset = "utf-8"
compress.encoding_mode = "base64"

compress.first_chunk = true
compress.last_chunk = false

local bdCompressed = chilkat.BinData.new()
local sbUncompressedChunk = chilkat.StringBuilder.new()

for i = 0, 24 do
    if i == 24 then
        compress.last_chunk = true
    end

    sbUncompressedChunk:clear()
    sbUncompressedChunk:append_int(i)
    sbUncompressedChunk:append(": This is a line of data to be compressed...\r\n")

    compress:compress_sb(sbUncompressedChunk, bdCompressed)

    compress.first_chunk = false
end

print("The base64 encoded compressed text:")
print(bdCompressed:get_encoded("base64"))

-- Decompress in one call:
local originalText = compress:decompress_string_enc(bdCompressed:get_encoded("base64"))
print(originalText)