Compressing Data

Data can be compressed (and decompressed) using the deflate (ZLIB RFC 1950) or the gzip algorithm. The following examples illustrate the use for both algorithms.

Compressing and decompressing data with the gzip algorithm
local cmp = require("syslib.gzip")

-- Compression
local dataString = "The data as a string"
local level = 5 -- integer 1..9 where 1 indicates the fastest and least compression
                -- and 9 indicates the slowest but strongest compression
local compressed -- the compressed data string
local ok_c -- success status of the compression (true/false)

compressed, ok_c = cmp:compress(dataString, level)


-- Decompression
local decompressed -- the string resulting from the decompression
local ok_d -- success status of the decompression (true/false)

decompressed, ok_d = gzip:decompress(compressed)
Compressing and decompressing data with the deflate (zlib) algorithm
local cmp = require("syslib.zlib")

-- Compression
local dataString = "The data as a string"
local level = 5 -- integer 1..9 where 1 indicates the fastest and least compression
                -- and 9 indicates the slowest but strongest compression
local noheader = false -- if noheader is set to true, then ZLIB data header is not used
local buffer_size = 2024 -- buffer_size is the size of the buffer used during compression. The minimal value of buffer_size is 128 and maximal value is 16777216. Set it to 0 or omit it and the default size of the buffer (4096)
local compressed -- the compressed data string
local ok_c -- success status of the compression (true/false)

compressed, ok_c = cmp:compress(dataString, level, noheader, buffer_size)


-- Decompression
local decompressed -- the string resulting from the decompression
local ok_d -- success status of the decompression (true/false)

-- If noheader was 'true' for the compression (see above), it also needs to be 'true' for the decompression.
-- Default is 'false'.

decompressed, ok_d = cmp:decompress(compressed, noheader)