コンテンツにスキップ

圧縮

gzip、deflate、zlib、brotli、zstdアルゴリズムを使用してデータを圧縮・解凍。

local compress = require("compress")

最も広くサポートされているフォーマット(RFC 1952)。

-- HTTPレスポンス用に圧縮
local body = json.encode(large_response)
local compressed, err = compress.gzip.encode(body)
if err then
return nil, err
end
-- Content-Encodingヘッダーを設定
res:set_header("Content-Encoding", "gzip")
res:write(compressed)
-- ストレージ用の最大圧縮
local archived = compress.gzip.encode(data, {level = 9})
-- リアルタイム用の高速圧縮
local fast = compress.gzip.encode(data, {level = 1})
パラメータ説明
datastring圧縮するデータ
optionstable?オプションのエンコードオプション

オプション {id=“gzip-compress-options”}

Section titled “オプション {id=“gzip-compress-options”}”
フィールド説明
levelinteger圧縮レベル 1-9(デフォルト: 6)

戻り値: string, error

-- HTTPリクエストを解凍
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
-- サイズ制限付きで解凍(zip爆弾を防止)
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
パラメータ説明
datastringGZIP圧縮データ
optionstable?オプションのデコードオプション

オプション {id=“gzip-decompress-options”}

Section titled “オプション {id=“gzip-decompress-options”}”
フィールド説明
max_sizeinteger最大解凍サイズ(バイト単位)(デフォルト: 128MB、最大: 1GB)

戻り値: string, error

テキストに最適な圧縮率(RFC 7932)。

-- 静的アセットとテキストコンテンツに最適
local compressed = compress.brotli.encode(html_content, {level = 11})
-- 圧縮されたアセットをキャッシュ
cache:set("static:" .. hash, compressed)
-- APIレスポンス用の適度な圧縮
local compressed = compress.brotli.encode(json_data, {level = 4})
パラメータ説明
datastring圧縮するデータ
optionstable?オプションのエンコードオプション

オプション {id=“brotli-compress-options”}

Section titled “オプション {id=“brotli-compress-options”}”
フィールド説明
levelinteger圧縮レベル 0-11(デフォルト: 6)

戻り値: string, error

local decompressed, err = compress.brotli.decode(compressed_data)
if err then
return nil, err
end
-- サイズ制限付き
local decompressed = compress.brotli.decode(data, {max_size = 50 * 1024 * 1024})
パラメータ説明
datastringBrotli圧縮データ
optionstable?オプションのデコードオプション

オプション {id=“brotli-decompress-options”}

Section titled “オプション {id=“brotli-decompress-options”}”
フィールド説明
max_sizeinteger最大解凍サイズ(バイト単位)(デフォルト: 128MB、最大: 1GB)

戻り値: string, error

良好な圧縮率での高速圧縮(RFC 8878)。

-- 速度と圧縮率のバランスが良い
local compressed = compress.zstd.encode(binary_data)
-- アーカイブ用の高圧縮
local archived = compress.zstd.encode(data, {level = 19})
-- リアルタイムストリーミング用の高速モード
local fast = compress.zstd.encode(data, {level = 1})
パラメータ説明
datastring圧縮するデータ
optionstable?オプションのエンコードオプション

オプション {id=“zstd-compress-options”}

Section titled “オプション {id=“zstd-compress-options”}”
フィールド説明
levelinteger圧縮レベル 1-22(デフォルト: 3)
dictstring?train_dict で生成した Zstd 辞書バイト(デフォルト: なし)

戻り値: string, error

local decompressed, err = compress.zstd.decode(compressed_data)
if err then
return nil, err
end
パラメータ説明
datastringZstandard圧縮データ
optionstable?オプションのデコードオプション

オプション {id=“zstd-decompress-options”}

Section titled “オプション {id=“zstd-decompress-options”}”
フィールド説明
max_sizeinteger最大解凍サイズ(バイト単位)(デフォルト: 128MB、最大: 1GB)
dictstring?Zstd 辞書バイト(エンコードに使用したものと一致する必要があります)

戻り値: string, error

サンプルデータから辞書を学習させると、似通った小さなペイロードを多数圧縮する際の圧縮率を改善できます。学習させた辞書を encode/decodedict オプションとして渡します — エンコードとデコードの両方で同じ辞書を使用する必要があります。

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 })
パラメータ説明
samplesstring[]学習サンプル(少なくとも 1 つは 8 バイト以上)
optionstable?size(integer、目標辞書バイト数、256-1048576、デフォルト 114688)、id(integer、デフォルト 0)、level(integer、1-22)

戻り値: string, error(辞書バイト)

パラメータ説明
dictstring辞書バイト

戻り値: table, error{id: integer, content_size: integer}

生のDEFLATE圧縮(RFC 1951)。他のフォーマットで内部的に使用。

local compressed = compress.deflate.encode(data, {level = 6})
パラメータ説明
datastring圧縮するデータ
optionstable?オプションのエンコードオプション

オプション {id=“deflate-compress-options”}

Section titled “オプション {id=“deflate-compress-options”}”
フィールド説明
levelinteger圧縮レベル 1-9(デフォルト: 6)

戻り値: string, error

local decompressed = compress.deflate.decode(compressed)
パラメータ説明
datastringDEFLATE圧縮データ
optionstable?オプションのデコードオプション

オプション {id=“deflate-decompress-options”}

Section titled “オプション {id=“deflate-decompress-options”}”
フィールド説明
max_sizeinteger最大解凍サイズ(バイト単位)(デフォルト: 128MB、最大: 1GB)

戻り値: string, error

ヘッダーとチェックサム付きDEFLATE(RFC 1950)。

local compressed = compress.zlib.encode(data, {level = 6})
パラメータ説明
datastring圧縮するデータ
optionstable?オプションのエンコードオプション

オプション {id=“zlib-compress-options”}

Section titled “オプション {id=“zlib-compress-options”}”
フィールド説明
levelinteger圧縮レベル 1-9(デフォルト: 6)

戻り値: string, error

local decompressed = compress.zlib.decode(compressed)
パラメータ説明
datastringZlib圧縮データ
optionstable?オプションのデコードオプション

オプション {id=“zlib-decompress-options”}

Section titled “オプション {id=“zlib-decompress-options”}”
フィールド説明
max_sizeinteger最大解凍サイズ(バイト単位)(デフォルト: 128MB、最大: 1GB)

戻り値: string, error

アルゴリズム最適な用途速度圧縮率レベル範囲
gzipHTTP、幅広い互換性良好1-9
brotli静的アセット、テキスト遅い最高0-11
zstd大きなファイル、ストリーミング高速良好1-22
deflate/zlib低レベル、特定のプロトコル良好1-9
-- Accept-Encodingに基づくHTTPレスポンス
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
条件種別再試行可能
空の入力errors.INVALIDno
レベルが範囲外errors.INVALIDno
無効な圧縮データerrors.INVALIDno
解凍サイズが制限を超過errors.INTERNALno

エラーの処理についてはエラー処理を参照。