LZ4 is a very fast compression and decompression algorithm. This Lua binding is in conformance with the LZ4 block and frame specifications and also support streaming.
Simple frame compression/decompression
local lz4 = require("lz4")
local s = "LZ4 is a very fast compression and decompression algorithm."
assert(lz4.decompress(lz4.compress(s)) == s)
With luarocks:
luarocks install lua-lz4
With make:
export LUA_INCDIR=/path/to/lua_header
export LUA_LIBDIR=/path/to/liblua
make
Easy to use and compressed data contain meta data such as checksum, decompress size, etc. The compressed/decompressed data in frame format can be exchange with other programs.
Compress input
and return compressed data.
input
: input string to be compressed.options
: optional table that can be containscompression_level
: integer between 0 to 16auto_flush
: booleanblock_size
: maximum block size can belz4.block_64KB
,lz4.block_256KB.
,lz4.block_1MB
,lz4.block_4MB
block_independent
: booleancontent_checksum
: boolean
Decompress input
and return decompressed data.
input
: input string to be decompressed.
Basic compression/decompression in plain block format. Require decompress_length
to decompress data.
Example:
local lz4 = require("lz4")
local s = "LZ4 is a very fast compression and decompression algorithm."
assert(lz4.block_decompress_safe(lz4.block_compress(s), #s) == s)
Compress input
and return compressed data.
input
: input string to be compressed.accelerate
: optional integer
Compress input
in high compression mode and return compressed data.
input
: input string to be compressed.compression_level
: optional integer
Decompress input
and return decompressed data. This function is protected against buffer overflow exploits, including malicious data packets.
input
: input string to be decompressed.decompress_length
: length of decompressed data (integer)
Decompress input
and return decompressed data. It does not provide any protection against intentionally modified data stream (malicious input). Use this function in trusted environment only (data to decode comes from a trusted source).
input
: input string to be decompressed.decompress_length
: length of decompressed data (integer)
Use streaming to compress/decompress multiple blocks. The compressing blocks can be use content in previous blocks therefore more compression ratio. Decoding buffer should be either to get good performance.
- Exactly same size as encoding buffer, with same update rule (block boundaries at same positions) In which case, the decoding & encoding ring buffer can have any size, including very small ones ( < 64 KB).
- Larger than encoding buffer, by a minimum of maxBlockSize more bytes. maxBlockSize is implementation dependent. It's the maximum size you intend to compress into a single block. In which case, encoding and decoding buffers do not need to be synchronized, and encoding ring buffer can have any size, including small ones ( < 64 KB).
- At least 64 KB + 8 bytes + maxBlockSize. In which case, encoding and decoding buffers do not need to be synchronized, and encoding ring buffer can have any size, including larger than decoding buffer.
Example:
local lz4 = require("lz4")
local s1 = "LZ4 is a very fast compression and decompression algorithm."
local s2 = "lua-lz4 - LZ4 binding for Lua"
local com = lz4.new_compression_stream()
local dec = lz4.new_decompression_stream()
assert(dec:decompress_safe(com:compress(s1), #s1) == s1)
assert(dec:decompress_safe(com:compress(s2), #s2) == s2)
New a lz4.compression_stream
object.
ring_buffer_size
: integeraccelerate
: integer
reset([dictionary])
forget internal dictionary or reset to new dictionarycompress(input)
New a lz4.compression_stream_hc
object.
ring_buffer_size
: integercompression_level
: integer
reset([dictionary])
forget internal dictionary or reset to new dictionarycompress(input)
New a lz4.decompression_stream
object.
ring_buffer_size
: integer
reset([dictionary])
forget internal dictionary or reset to new dictionarydecompress_safe(input, decompress_length)
decompress_fast(input, decompress_length)