Skip to main content

Pure Python implementation of the XZ file format with random access support

Project description

python-xz

Pure Python implementation of the XZ file format with random access support

GitHub build status Release on PyPI Code coverage MIT License


:book: Documentation   |   :page_with_curl: Changelog


A XZ file can be composed of several streams and blocks. This allows for random access when reading, but this is not supported by Python's builtin lzma module, which would read all previous blocks for nothing.

lzma lzmaffi python-xz
Module type builtin cffi (C extension) native
Random access (read) :heavy_multiplication_x: no :heavy_check_mark: yes :heavy_check_mark: yes
Several streams (read) :heavy_check_mark: yes :heavy_check_mark: yes :heavy_check_mark: yes
Several streams (write) :heavy_multiplication_x: no :heavy_multiplication_x: no :hourglass: planned
Several blocks (read) :heavy_check_mark: yes :heavy_check_mark: yes :heavy_check_mark: yes
Several blocks (write) :heavy_multiplication_x: no :heavy_multiplication_x: no :hourglass: planned
Stream padding (read) :heavy_multiplication_x: no :heavy_check_mark: yes :heavy_check_mark: yes
Stream padding (write) :heavy_multiplication_x: no :heavy_multiplication_x: no :hourglass: planned

Usage

Read mode

The API is similar to lzma: you can use either xz.open or xz.XZFile.

>>> with xz.open('example.xz') as fin:
...     fin.read(18)
...     fin.stream_boundaries  # 2 streams
...     fin.block_boundaries   # 4 blocks in first stream, 2 blocks in second stream
...     fin.seek(1000)
...     fin.read(31)
...
b'Hello, world! \xf0\x9f\x91\x8b'
[0, 2000]
[0, 500, 1000, 1500, 2000, 3000]
1000
b'\xe2\x9c\xa8 Random access is fast! \xf0\x9f\x9a\x80'

Opening in text mode works as well, but notice that seek arguments as well as boundaries are still in bytes (just like with lzma.open).

>>> with xz.open('example.xz', 'rt') as fin:
...     fin.read(15)
...     fin.stream_boundaries
...     fin.block_boundaries
...     fin.seek(1000)
...     fin.read(26)
...
'Hello, world! 👋'
[0, 2000]
[0, 500, 1000, 1500, 2000, 3000]
1000
'✨ Random access is fast! 🚀'

Write mode

This mode is not available yet.


FAQ

How does random-access works?

XZ files are made of a number of streams, and each stream is composed of a number of block. This can be seen with xz --list:

$ xz --list file.xz
Strms  Blocks   Compressed Uncompressed  Ratio  Check   Filename
    1      13     16.8 MiB    297.9 MiB  0.056  CRC64   file.xz

To read data from the middle of the 10th block, we will decompress the 10th block from its start it until we reach the middle (and drop that decompressed data), then returned the decompressed data from that point.

Choosing the good block size is a tradeoff between seeking time during random access and compression ratio.

How can I create XZ files optimized for random-access?

XZ Utils can create XZ files with several blocks:

$ xz -T0 file                          # threading mode
$ xz --block-size 16M file             # same size for all blocks
$ xz --block-list 16M,32M,8M,42M file  # specific size for each block

PIXZ creates files with several blocks by default:

$ pixz file

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python-xz-0.1.0.tar.gz (49.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_xz-0.1.0-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file python-xz-0.1.0.tar.gz.

File metadata

  • Download URL: python-xz-0.1.0.tar.gz
  • Upload date:
  • Size: 49.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

File hashes

Hashes for python-xz-0.1.0.tar.gz
Algorithm Hash digest
SHA256 02240cb66dbe192efbae0f63d0513bc49e948cb786d6419d90b869a47449cc50
MD5 7bbd22f15ce30bda26a229554fe1670a
BLAKE2b-256 9a4557bc046922ece281ac3dd15a0cfc71fccdcdb1d4e93310bb9dbed8ff8cae

See more details on using hashes here.

File details

Details for the file python_xz-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: python_xz-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

File hashes

Hashes for python_xz-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e10e57e33fd0d7e091459fdc5683c372885cef13d07070928c54d5b1e069d778
MD5 4feadbda6b28689f2fbb87f12b71591d
BLAKE2b-256 cb5c05afec566c76e2450e8f4dc141e0ff9767d376038c2b1425bc2ea88d8142

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page