| 1 |
.. py:currentmodule:: lz4.frame |
| 2 |
.. default-role:: obj |
| 3 |
|
| 4 |
Quickstart |
| 5 |
========== |
| 6 |
|
| 7 |
Simple usage |
| 8 |
------------ |
| 9 |
|
| 10 |
The recommended binding to use is the LZ4 frame format binding, since this |
| 11 |
provides interoperability with other implementations and language bindings. |
| 12 |
|
| 13 |
The simplest way to use the frame bindings is via the :py:func:`compress` and |
| 14 |
:py:func:`decompress` functions: |
| 15 |
|
| 16 |
.. doctest:: |
| 17 |
|
| 18 |
>>> import os |
| 19 |
>>> import lz4.frame |
| 20 |
>>> input_data = 20 * 128 * os.urandom(1024) # Read 20 * 128kb |
| 21 |
>>> compressed = lz4.frame.compress(input_data) |
| 22 |
>>> decompressed = lz4.frame.decompress(compressed) |
| 23 |
>>> decompressed == input_data |
| 24 |
True |
| 25 |
|
| 26 |
The :py:func:`compress` function reads the input data and compresses it and |
| 27 |
returns a LZ4 frame. A frame consists of a header, and a sequence of blocks of |
| 28 |
compressed data, and a frame end marker (and optionally a checksum of the |
| 29 |
uncompressed data). The :py:func:`decompress` function takes a full LZ4 frame, |
| 30 |
decompresses it (and optionally verifies the uncompressed data against the |
| 31 |
stored checksum), and returns the uncompressed data. |
| 32 |
|
| 33 |
Working with data in chunks |
| 34 |
--------------------------- |
| 35 |
|
| 36 |
It's often inconvenient to hold the full data in memory, and so functions are |
| 37 |
also provided to compress and decompress data in chunks: |
| 38 |
|
| 39 |
.. doctest:: |
| 40 |
|
| 41 |
>>> import lz4.frame |
| 42 |
>>> import os |
| 43 |
>>> input_data = 20 * 128 * os.urandom(1024) |
| 44 |
>>> c_context = lz4.frame.create_compression_context() |
| 45 |
>>> compressed = lz4.frame.compress_begin(c_context) |
| 46 |
>>> compressed += lz4.frame.compress_chunk(c_context, input_data[:10 * 128 * 1024]) |
| 47 |
>>> compressed += lz4.frame.compress_chunk(c_context, input_data[10 * 128 * 1024:]) |
| 48 |
>>> compressed += lz4.frame.compress_flush(c_context) |
| 49 |
|
| 50 |
Here a compression context is first created which is used to maintain state |
| 51 |
across calls to the LZ4 library. This is an opaque PyCapsule object. |
| 52 |
:py:func:`compress_begin` starts a new frame and returns the frame header. |
| 53 |
:py:func:`compress_chunk` compresses input data and returns the compressed data. |
| 54 |
:py:func:`compress_flush` ends the frame and returns the frame end marker. The |
| 55 |
data returned from these functions is catenated to form the compressed frame. |
| 56 |
|
| 57 |
:py:func:`compress_flush` also flushes any buffered data; by default, |
| 58 |
:py:func:`compress_chunk` may buffer data until a block is full. This buffering |
| 59 |
can be disabled by specifying ``auto_flush=True`` when calling |
| 60 |
:py:func:`compress_begin`. Alternatively, the LZ4 buffers can be flushed at any |
| 61 |
time without ending the frame by calling :py:func:`compress_flush` with |
| 62 |
``end_frame=False``. |
| 63 |
|
| 64 |
Decompressing data can also be done in a chunked fashion: |
| 65 |
|
| 66 |
.. doctest:: |
| 67 |
|
| 68 |
>>> d_context = lz4.frame.create_decompression_context() |
| 69 |
>>> d1, b, e = lz4.frame.decompress_chunk(d_context, compressed[:len(compressed)//2]) |
| 70 |
>>> d2, b, e = lz4.frame.decompress_chunk(d_context, compressed[len(compressed)//2:]) |
| 71 |
>>> d1 + d2 == input_data |
| 72 |
True |
| 73 |
|
| 74 |
Note that :py:func:`decompress_chunk` returns a tuple ``(decompressed_data, |
| 75 |
bytes_read, end_of_frame_indicator)``. ``decompressed_data`` is the decompressed |
| 76 |
data, ``bytes_read`` reports the number of bytes read from the compressed input. |
| 77 |
``end_of_frame_indicator`` is ``True`` if the end-of-frame marker is encountered |
| 78 |
during the decompression, and ``False`` otherwise. If the end-of-frame marker is |
| 79 |
encountered in the input, no attempt is made to decompress the data after the |
| 80 |
marker. |
| 81 |
|
| 82 |
Rather than managing compression and decompression context objects manually, it |
| 83 |
is more convenient to use the :py:class:`LZ4FrameCompressor` and |
| 84 |
:py:class:`LZ4FrameDecompressor` classes which provide context manager |
| 85 |
functionality: |
| 86 |
|
| 87 |
.. doctest:: |
| 88 |
|
| 89 |
>>> import lz4.frame |
| 90 |
>>> import os |
| 91 |
>>> input_data = 20 * 128 * os.urandom(1024) |
| 92 |
>>> with lz4.frame.LZ4FrameCompressor() as compressor: |
| 93 |
... compressed = compressor.begin() |
| 94 |
... compressed += compressor.compress(input_data[:10 * 128 * 1024]) |
| 95 |
... compressed += compressor.compress(input_data[10 * 128 * 1024:]) |
| 96 |
... compressed += compressor.flush() |
| 97 |
>>> with lz4.frame.LZ4FrameDecompressor() as decompressor: |
| 98 |
... decompressed = decompressor.decompress(compressed[:len(compressed)//2]) |
| 99 |
... decompressed += decompressor.decompress(compressed[len(compressed)//2:]) |
| 100 |
>>> decompressed == input_data |
| 101 |
True |
| 102 |
|
| 103 |
|
| 104 |
Working with compressed files |
| 105 |
----------------------------- |
| 106 |
|
| 107 |
The frame bindings provide capability for working with files containing LZ4 |
| 108 |
frame compressed data. This functionality is intended to be a drop in |
| 109 |
replacement for that offered in the Python standard library for bz2, gzip and |
| 110 |
LZMA compressed files. The :py:func:`lz4.frame.open()` function is the most |
| 111 |
convenient way to work with compressed data files: |
| 112 |
|
| 113 |
.. doctest:: |
| 114 |
|
| 115 |
>>> import lz4.frame |
| 116 |
>>> import os |
| 117 |
>>> input_data = 20 * os.urandom(1024) |
| 118 |
>>> with lz4.frame.open('testfile', mode='wb') as fp: |
| 119 |
... bytes_written = fp.write(input_data) |
| 120 |
... bytes_written == len(input_data) |
| 121 |
True |
| 122 |
>>> with lz4.frame.open('testfile', mode='r') as fp: |
| 123 |
... output_data = fp.read() |
| 124 |
>>> output_data == input_data |
| 125 |
True |
| 126 |
|
| 127 |
The library also provides the class :py:class:`lz4.frame.LZ4FrameFile` for |
| 128 |
working with compressed files. |
| 129 |
|
| 130 |
|
| 131 |
Controlling the compression |
| 132 |
--------------------------- |
| 133 |
|
| 134 |
Beyond the basic usage described above, there are a number of keyword arguments |
| 135 |
to tune and control the compression. A few of the key ones are listed below, |
| 136 |
please see the documentation for full details of options. |
| 137 |
|
| 138 |
|
| 139 |
Controlling the compression level |
| 140 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 141 |
|
| 142 |
The ``compression_level`` argument specifies the level of compression used with |
| 143 |
0 (default) being the lowest compression (0-2 are the same value), and 16 the |
| 144 |
highest compression. Values below 0 will enable "fast acceleration", |
| 145 |
proportional to the value. Values above 16 will be treated as 16. The following |
| 146 |
module constants are provided as a convenience: |
| 147 |
|
| 148 |
- `lz4.frame.COMPRESSIONLEVEL_MIN`: Minimum compression (0, default) |
| 149 |
- `lz4.frame.COMPRESSIONLEVEL_MINHC`: Minimum high-compression mode (3) |
| 150 |
- `lz4.frame.COMPRESSIONLEVEL_MAX`: Maximum compression (16) |
| 151 |
|
| 152 |
Availability: :py:func:`lz4.frame.compress()`, |
| 153 |
:py:func:`lz4.frame.compress_begin()`, :py:func:`lz4.frame.open()`, |
| 154 |
:py:class:`lz4.frame.LZ4FrameCompressor`, :py:class:`lz4.frame.LZ4FrameFile`. |
| 155 |
|
| 156 |
|
| 157 |
Controlling the block size |
| 158 |
~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 159 |
|
| 160 |
The ``block_size`` argument specifies the maximum block size to use for the |
| 161 |
blocks in a frame. Options: |
| 162 |
|
| 163 |
- `lz4.frame.BLOCKSIZE_DEFAULT` or 0: the lz4 library default |
| 164 |
- `lz4.frame.BLOCKSIZE_MAX64KB` or 4: 64 kB |
| 165 |
- `lz4.frame.BLOCKSIZE_MAX256KB` or 5: 256 kB |
| 166 |
- `lz4.frame.BLOCKSIZE_MAX1MB` or 6: 1 MB |
| 167 |
- `lz4.frame.BLOCKSIZE_MAX4MB` or 7: 4 MB |
| 168 |
|
| 169 |
If unspecified, will default to `lz4.frame.BLOCKSIZE_DEFAULT` which is |
| 170 |
currently equal to `lz4.frame.BLOCKSIZE_MAX64KB` |
| 171 |
|
| 172 |
Availability: :py:func:`lz4.frame.compress()`, |
| 173 |
:py:func:`lz4.frame.compress_begin()`, :py:func:`lz4.frame.open()`, |
| 174 |
:py:class:`lz4.frame.LZ4FrameCompressor`, :py:class:`lz4.frame.LZ4FrameFile`. |
| 175 |
|
| 176 |
|
| 177 |
Controlling block linking |
| 178 |
~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 179 |
|
| 180 |
The ``block_linked`` argument specifies whether to use block-linked compression. |
| 181 |
If ``True``, the compression process will use data between sequential blocks to |
| 182 |
improve the compression ratio, particularly for small blocks. The default is |
| 183 |
``True``. |
| 184 |
|
| 185 |
Availability: :py:func:`lz4.frame.compress()`, |
| 186 |
:py:func:`lz4.frame.compress_begin()`, :py:func:`lz4.frame.open()`, |
| 187 |
:py:class:`lz4.frame.LZ4FrameCompressor`, :py:class:`lz4.frame.LZ4FrameFile`. |
| 188 |
|
| 189 |
|
| 190 |
Data checksum validation |
| 191 |
~~~~~~~~~~~~~~~~~~~~~~~~ |
| 192 |
|
| 193 |
The ``content_checksum`` argument specifies whether to enable checksumming of |
| 194 |
the uncompressed content. If ``True``, a checksum of the uncompressed data is |
| 195 |
stored at the end of the frame, and checked during decompression. Default is |
| 196 |
``False``. |
| 197 |
|
| 198 |
The ``block_checksum`` argument specifies whether to enable checksumming of the |
| 199 |
uncompressed content of each individual block in the frame. If ``True``, a |
| 200 |
checksum is stored at the end of each block in the frame, and checked during |
| 201 |
decompression. Default is ``False``. |
| 202 |
|
| 203 |
Availability: :py:func:`lz4.frame.compress()`, |
| 204 |
:py:func:`lz4.frame.compress_begin()`, :py:func:`lz4.frame.open()`, |
| 205 |
:py:class:`lz4.frame.LZ4FrameCompressor`, :py:class:`lz4.frame.LZ4FrameFile`. |
| 206 |
|
| 207 |
|
| 208 |
Data buffering |
| 209 |
~~~~~~~~~~~~~~ |
| 210 |
|
| 211 |
The LZ4 library can be set to buffer data internally until a block is filed in |
| 212 |
order to optimize compression. The ``auto_flush`` argument specifies whether the |
| 213 |
library should buffer input data or not. |
| 214 |
|
| 215 |
When ``auto_flush`` is ``False`` the LZ4 library may buffer data internally. In |
| 216 |
this case, the compression functions may return no compressed data when called. |
| 217 |
This is the default. |
| 218 |
|
| 219 |
When ``auto_flush`` is ``True``, the compression functions will return |
| 220 |
compressed data immediately. |
| 221 |
|
| 222 |
Availability: :py:func:`lz4.frame.compress()`, |
| 223 |
:py:func:`lz4.frame.compress_begin()`, :py:func:`lz4.frame.open()`, |
| 224 |
:py:class:`lz4.frame.LZ4FrameCompressor`, :py:class:`lz4.frame.LZ4FrameFile`. |
| 225 |
|
| 226 |
|
| 227 |
Storing the uncompressed source data size in the frame |
| 228 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 229 |
|
| 230 |
The ``store_size`` and ``source_size`` arguments allow for storing the size of |
| 231 |
the uncompressed data in the frame header. Storing the source size in the frame |
| 232 |
header adds an extra 8 bytes to the size of the compressed frame, but allows the |
| 233 |
decompression functions to better size memory buffers during decompression. |
| 234 |
|
| 235 |
If ``store_size`` is ``True`` the size of the uncompressed data will be stored in |
| 236 |
the frame header. Default is ``True``. |
| 237 |
|
| 238 |
Availability of ``store_size``: :py:func:`lz4.frame.compress()` |
| 239 |
|
| 240 |
The ``source_size`` argument optionally specifies the uncompressed size of the |
| 241 |
source data to be compressed. If specified, the size will be stored in the frame |
| 242 |
header. |
| 243 |
|
| 244 |
Availability of ``source_size``: :py:meth:`lz4.frame.LZ4FrameCompressor.begin()`, |
| 245 |
:py:func:`lz4.frame.compress_begin()`, :py:func:`lz4.frame.open()`, |
| 246 |
:py:class:`lz4.frame.LZ4FrameFile`. |
| 247 |
|
| 248 |
|
| 249 |
Working with streamed compressed data |
| 250 |
------------------------------------- |
| 251 |
|
| 252 |
The stream bindings provide capability for working with stream compressed LZ4 |
| 253 |
data. This functionality is based on the usage of a ring-buffer (not implemented |
| 254 |
yet) or a double-buffer, with the length of each block preceding the compressed |
| 255 |
payload in the stream. |
| 256 |
|
| 257 |
The stream compression reuses a context between each processed block for |
| 258 |
performance gain. |
| 259 |
|
| 260 |
Most of the arguments used to initialize the LZ4 stream context are shared with |
| 261 |
the block API. Hereafter, those specific to the LZ4 stream API are detailed. |
| 262 |
|
| 263 |
|
| 264 |
Controlling the buffer size |
| 265 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 266 |
|
| 267 |
The ``buffer_size`` argument represents the base buffer size used internally for |
| 268 |
memory allocation: |
| 269 |
|
| 270 |
* In the case of the double-buffer strategy, this is the size of each buffer of |
| 271 |
the double-buffer. |
| 272 |
|
| 273 |
When compressing, this size is the maximal length of the input uncompressed |
| 274 |
chunks. |
| 275 |
|
| 276 |
When decompressing, this size is the maximal length of the decompressed data. |
| 277 |
|
| 278 |
|
| 279 |
Storing the compressed data size in the block |
| 280 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 281 |
|
| 282 |
The ``store_comp_size`` argument allows tuning of the size (in bytes) of the |
| 283 |
compressed block, which is prepended to the actual LZ4 compressed payload. |
| 284 |
This size can be either on ``1``, ``2`` or ``4`` bytes, or ``0`` for out-of-band |
| 285 |
block size record. |