Skip to content

Compression

Compress and decompress data using gzip, deflate, zlib, brotli, and zstd algorithms.

local compress = require("compress")

Most widely supported format (RFC 1952).

-- Compress for HTTP response
local body = json.encode(large_response)
local compressed, err = compress.gzip.encode(body)
if err then
return nil, err
end
-- Set Content-Encoding header
res:set_header("Content-Encoding", "gzip")
res:write(compressed)
-- Maximum compression for storage
local archived = compress.gzip.encode(data, {level = 9})
-- Fast compression for real-time
local fast = compress.gzip.encode(data, {level = 1})
ParameterTypeDescription
datastringData to compress
optionstable?Optional encoding options
FieldTypeDescription
levelintegerCompression level 1-9 (default: 6)

Returns: string, error

-- Decompress HTTP request
local content_encoding = req:header("Content-Encoding")
if content_encoding == "gzip" then
local body = req:body()
local decompressed, err = compress.gzip.decode(body)
if err then
return nil, errors.new("INVALID", "Invalid gzip data")
end
body = decompressed
end
-- Decompress with size limit (prevent zip bombs)
local decompressed, err = compress.gzip.decode(data, {max_size = 10 * 1024 * 1024})
if err then
return nil, errors.new("INVALID", "Decompressed size exceeds 10MB limit")
end
ParameterTypeDescription
datastringGZIP compressed data
optionstable?Optional decoding options

Options {id=“gzip-decompress-options”}

Section titled “Options {id=“gzip-decompress-options”}”
FieldTypeDescription
max_sizeintegerMax decompressed size in bytes (default: 128MB, max: 1GB)

Returns: string, error

Best compression ratio for text (RFC 7932).

-- Best for static assets and text content
local compressed = compress.brotli.encode(html_content, {level = 11})
-- Cache compressed assets
cache:set("static:" .. hash, compressed)
-- Moderate compression for API responses
local compressed = compress.brotli.encode(json_data, {level = 4})
ParameterTypeDescription
datastringData to compress
optionstable?Optional encoding options

Options {id=“brotli-compress-options”}

Section titled “Options {id=“brotli-compress-options”}”
FieldTypeDescription
levelintegerCompression level 0-11 (default: 6)

Returns: string, error

local decompressed, err = compress.brotli.decode(compressed_data)
if err then
return nil, err
end
-- With size limit
local decompressed = compress.brotli.decode(data, {max_size = 50 * 1024 * 1024})
ParameterTypeDescription
datastringBrotli compressed data
optionstable?Optional decoding options

Options {id=“brotli-decompress-options”}

Section titled “Options {id=“brotli-decompress-options”}”
FieldTypeDescription
max_sizeintegerMax decompressed size in bytes (default: 128MB, max: 1GB)

Returns: string, error

Fast compression with good ratios (RFC 8878).

-- Good balance of speed and ratio
local compressed = compress.zstd.encode(binary_data)
-- Higher compression for archival
local archived = compress.zstd.encode(data, {level = 19})
-- Fast mode for real-time streaming
local fast = compress.zstd.encode(data, {level = 1})
ParameterTypeDescription
datastringData to compress
optionstable?Optional encoding options
FieldTypeDescription
levelintegerCompression level 1-22 (default: 3)
dictstring?Zstd dictionary bytes from train_dict (default: none)

Returns: string, error

local decompressed, err = compress.zstd.decode(compressed_data)
if err then
return nil, err
end
ParameterTypeDescription
datastringZstandard compressed data
optionstable?Optional decoding options

Options {id=“zstd-decompress-options”}

Section titled “Options {id=“zstd-decompress-options”}”
FieldTypeDescription
max_sizeintegerMax decompressed size in bytes (default: 128MB, max: 1GB)
dictstring?Zstd dictionary bytes (must match the dict used to encode)

Returns: string, error

Train a dictionary from sample data to improve compression of many small, similar payloads. Pass the trained dictionary as the dict option to encode/decode — the same dictionary must be used for both.

local dict, err = compress.zstd.train_dict(samples, { size = 112640 })
local packed = compress.zstd.encode(data, { dict = dict })
local original = compress.zstd.decode(packed, { dict = dict })
ParameterTypeDescription
samplesstring[]Training samples (at least one >= 8 bytes)
optionstable?size (integer, target dict bytes, 256-1048576, default 114688), id (integer, default 0), level (integer, 1-22)

Returns: string, error (the dictionary bytes)

ParameterTypeDescription
dictstringDictionary bytes

Returns: table, error{id: integer, content_size: integer}

Raw DEFLATE compression (RFC 1951). Used internally by other formats.

local compressed = compress.deflate.encode(data, {level = 6})
ParameterTypeDescription
datastringData to compress
optionstable?Optional encoding options

Options {id=“deflate-compress-options”}

Section titled “Options {id=“deflate-compress-options”}”
FieldTypeDescription
levelintegerCompression level 1-9 (default: 6)

Returns: string, error

local decompressed = compress.deflate.decode(compressed)
ParameterTypeDescription
datastringDEFLATE compressed data
optionstable?Optional decoding options

Options {id=“deflate-decompress-options”}

Section titled “Options {id=“deflate-decompress-options”}”
FieldTypeDescription
max_sizeintegerMax decompressed size in bytes (default: 128MB, max: 1GB)

Returns: string, error

DEFLATE with header and checksum (RFC 1950).

local compressed = compress.zlib.encode(data, {level = 6})
ParameterTypeDescription
datastringData to compress
optionstable?Optional encoding options
FieldTypeDescription
levelintegerCompression level 1-9 (default: 6)

Returns: string, error

local decompressed = compress.zlib.decode(compressed)
ParameterTypeDescription
datastringZlib compressed data
optionstable?Optional decoding options

Options {id=“zlib-decompress-options”}

Section titled “Options {id=“zlib-decompress-options”}”
FieldTypeDescription
max_sizeintegerMax decompressed size in bytes (default: 128MB, max: 1GB)

Returns: string, error

AlgorithmBest ForSpeedRatioLevel Range
gzipHTTP, wide compatibilityMediumGood1-9
brotliStatic assets, textSlowBest0-11
zstdLarge files, streamingFastGood1-22
deflate/zlibLow-level, specific protocolsMediumGood1-9
-- HTTP response based on Accept-Encoding
local accept = req:header("Accept-Encoding") or ""
local body = json.encode(response_data)
if accept:find("br") then
res:set_header("Content-Encoding", "br")
res:write(compress.brotli.encode(body))
elseif accept:find("gzip") then
res:set_header("Content-Encoding", "gzip")
res:write(compress.gzip.encode(body))
else
res:write(body)
end
ConditionKindRetryable
Empty inputerrors.INVALIDno
Level out of rangeerrors.INVALIDno
Invalid compressed dataerrors.INVALIDno
Decompressed size exceeds limiterrors.INTERNALno

See Error Handling for working with errors.