Submodule compress.zlib¶
Overview¶
The compress.zlib
submodule provides the ability to compress and decompress data using the zlib algorithm.
You can use the zlib
compressor as follows:
Create a compressor instance using the compress.zlib.new() function:
local zlib_compressor = require('compress.zlib').new() -- or -- local zlib_compressor = require('compress').zlib.new()
Optionally, you can pass compression options (zlib_opts) specific for
zlib
:local zlib_compressor = require('compress.zlib').new({ level = 5, mem_level = 5, strategy = 'filtered' })
To compress the specified data, use the compress() method:
compressed_data = zlib_compressor:compress('Hello world!')
To decompress data, call decompress():
decompressed_data = zlib_compressor:decompress(compressed_data)
API Reference¶
Functions |
|
Create a |
|
Objects |
|
A |
|
Configuration options of the |
compress.zlib.new()¶
- new([zlib_opts])¶
Create a
zlib
compressor instance.- Параметры
options (table) –
zlib
compression options (see zlib_opts)- Результат
a new
zlib
compressor instance (see zlib_compressor)- Тип результата
userdata
Example
local zlib_compressor = require('compress.zlib').new({ level = 5, mem_level = 5, strategy = 'filtered' })
zlib_compressor¶
- class zlib_compressor¶
A compressor instance that exposes the API for compressing and decompressing data using the
zlib
algorithm. To create thezlib
compressor, call compress.zlib.new().- compress(data)¶
Compress the specified data.
- Параметры
data (string) – data to be compressed
- Результат
compressed data
- Тип результата
string
Example
compressed_data = zlib_compressor:compress('Hello world!')
- decompress(data)¶
Decompress the specified data.
- Параметры
data (string) – data to be decompressed
- Результат
decompressed data
- Тип результата
string
Example
decompressed_data = zlib_compressor:decompress(compressed_data)
zlib_opts¶
- class zlib_opts¶
Configuration options of the zlib_compressor. These options can be passed to the compress.zlib.new() function.
Example
local zlib_compressor = require('compress.zlib').new({ level = 5, mem_level = 5, strategy = 'filtered' })
- level¶
Specifies the
zlib
compression level that enables you to adjust the compression ratio and speed. The lower level improves the compression speed at the cost of compression ratio.Default: 6Minimum: 0 (no compression)Maximum: 9
- mem_level¶
Specifies how much memory is allocated for the
zlib
compressor. The larger value improves the compression speed and ratio.Default: 8Minimum: 1Maximum: 9
- strategy¶
Specifies the compression strategy. The possible values:
default
- for normal data.huffman_only
- forces Huffman encoding only (no string match). The fastest compression algorithm but not very effective in compression for most of the data.filtered
- for data produced by a filter or predictor. Filtered data consists mostly of small values with a somewhat random distribution. This compression algorithm is tuned to compress them better.rle
- limits match distances to one (run-length encoding).rle
is designed to be almost as fast ashuffman_only
but gives better compression for PNG image data.fixed
- prevents the use of dynamic Huffman codes and provides a simpler decoder for special applications.