Search code examples
python-3.xpython-3.7lzmatarfile

How to set compression level while using `tarfile` with LZMA compression?


Is there a way to set compression level while creating an archive using tarfile module in Python for LZMA (*.xz) compression?

I am using the following construct and I am wondering if compresslevel keyword argument for tarfile.open method applies to LZMA compression too?

tarfile.open(tar_file_path, 'w:xz', compresslevel=9) as tf:
    ...

The documentation is somewhat unhelpful on this...


Solution

  • Looks like 'compresslevel' is not a option in 'tarfile'.

    Using 'lzma' module it is possible.

    The compression settings can be specified either as a preset compression level (with the preset argument), or in detail as a custom filter chain (with the filters argument).

    The preset argument (if provided) should be an integer between 0 and 9 (inclusive), optionally OR-ed with the constant PRESET_EXTREME. If neither preset nor filters are given, the default behavior is to use PRESET_DEFAULT (preset level 6). Higher presets produce smaller output, but make the compression process slower.

    import lzma
    my_filters = [
        {"id": lzma.FILTER_DELTA, "dist": 5},
        {"id": lzma.FILTER_LZMA2, "preset": 7 | lzma.PRESET_EXTREME},
    ]
    with lzma.open("file.xz", "w", filters=my_filters) as f:
        f.write(b"blah blah blah")
    

    Please check Compression using the LZMA algorithm