| 1 | <div align="center" size="15px"> |
| 2 | |
| 3 | # backports.zstd |
| 4 | |
| 5 | Backport of [PEP-784 “adding Zstandard to the standard library”][PEP-784] |
| 6 | |
| 7 | [](https://github.com/rogdham/backports.zstd/actions?query=branch:master) |
| 8 | [](https://pypi.org/project/backports.zstd/) |
| 9 | |
| 10 | --- |
| 11 | |
| 12 | [📖 PEP-784][PEP-784] | [📃 Changelog](./CHANGELOG.md) |
| 13 | |
| 14 | [PEP-784]: https://peps.python.org/pep-0784/ |
| 15 | |
| 16 | </div> |
| 17 | |
| 18 | --- |
| 19 | |
| 20 | ## Install |
| 21 | |
| 22 | Add the following dependency to your project: |
| 23 | |
| 24 | ``` |
| 25 | backports.zstd ; python_version<'3.14' |
| 26 | ``` |
| 27 | |
| 28 | …or just run `pip install backports.zstd`. |
| 29 | |
| 30 | ## Usage |
| 31 | |
| 32 | When importing a module needing Zstandard support, use a conditional import based on the |
| 33 | version of Python. See below for examples. |
| 34 | |
| 35 | ### zstd |
| 36 | |
| 37 | ```python |
| 38 | import sys |
| 39 | |
| 40 | if sys.version_info >= (3, 14): |
| 41 | from compression import zstd |
| 42 | else: |
| 43 | from backports import zstd |
| 44 | |
| 45 | |
| 46 | # use the zstd module, for example: |
| 47 | zstd.compress(b"Hello, world!") |
| 48 | ``` |
| 49 | |
| 50 | Refer to the [official Python documentation][doc-zstd] for usage of the module. |
| 51 | |
| 52 | [doc-zstd]: https://docs.python.org/3.14/library/compression.zstd.html |
| 53 | |
| 54 | ### tarfile |
| 55 | |
| 56 | ```python |
| 57 | import sys |
| 58 | |
| 59 | if sys.version_info >= (3, 14): |
| 60 | import tarfile |
| 61 | else: |
| 62 | from backports.zstd import tarfile |
| 63 | |
| 64 | |
| 65 | # use the tarfile module, for example: |
| 66 | with tarfile.open("archive.tar.zst") as tar: |
| 67 | tar.list() |
| 68 | ``` |
| 69 | |
| 70 | This `tarfile` modules is backported from Python 3.14 and includes Zstandard-specific |
| 71 | features such as: explicit modes for opening files (e.g. `r:zstd`), specific arguments |
| 72 | (e.g. `zstd_dict`)… refer to the [official Python documentation][doc-tarfile] for more |
| 73 | info. |
| 74 | |
| 75 | [doc-tarfile]: https://docs.python.org/3.14/library/tarfile.html |
| 76 | |
| 77 | Moreover, the CLI is available as well: `python -m backports.zstd.tarfile`. |
| 78 | |
| 79 | ### zipfile |
| 80 | |
| 81 | ```python |
| 82 | import sys |
| 83 | |
| 84 | if sys.version_info >= (3, 14): |
| 85 | import zipfile |
| 86 | else: |
| 87 | from backports.zstd import zipfile |
| 88 | |
| 89 | |
| 90 | # use the zipfile module, for example: |
| 91 | with zipfile.ZipFile("archive.zip", "w") as zf: |
| 92 | zf.writestr("hello.txt", "Hi!", zipfile.ZIP_ZSTANDARD) |
| 93 | ``` |
| 94 | |
| 95 | This `zipfile` modules is backported from Python 3.14 and includes Zstandard-specific |
| 96 | features such as the constant `ZIP_ZSTANDARD` to be used for `compress_type`… refer to |
| 97 | the [official Python documentation][doc-zipfile] for more info. |
| 98 | |
| 99 | [doc-zipfile]: https://docs.python.org/3.14/library/zipfile.html |
| 100 | |
| 101 | Moreover, the CLI is available as well: `python -m backports.zstd.zipfile`. |
| 102 | |
| 103 | ### shutil |
| 104 | |
| 105 | ```python |
| 106 | import shutil |
| 107 | import sys |
| 108 | |
| 109 | if sys.version_info < (3, 14): |
| 110 | from backports.zstd import register_shutil |
| 111 | register_shutil() |
| 112 | |
| 113 | # use the shutil module, for example |
| 114 | shutil.unpack_archive('archive.tar.zst') |
| 115 | ``` |
| 116 | |
| 117 | Calling the `register_shutil` function allows to create zstd'ed tar files using the |
| 118 | `"zstdtar"` format, as well as unpack them. |
| 119 | |
| 120 | It also overrides support for unpacking zip files, enabling the unpacking of zip |
| 121 | archives that use Zstandard for compression. |
| 122 | |
| 123 | Alternatively, call `register_shutil(tar=False)` or `register_shutil(zip=False)` to |
| 124 | choose which archiving support to register. |
| 125 | |
| 126 | ## FAQ |
| 127 | |
| 128 | ### Who are you? |
| 129 | |
| 130 | This project is created and maintained by [Rogdham](https://github.com/rogdham) |
| 131 | (maintainer of [`pyzstd`](https://github.com/rogdham/pyzstd), who helped with [PEP-784] |
| 132 | and integration of Zstandard into the standard library), with help from |
| 133 | [Emma Smith](https://github.com/emmatyping) (author of [PEP-784], who did most of the |
| 134 | work of porting `pyzstd` into the standard library). |
| 135 | |
| 136 | ### How is this backport constructed? |
| 137 | |
| 138 | The aim is to be as close as possible to the upstream code of |
| 139 | [CPython](https://github.com/python/cpython). |
| 140 | |
| 141 | The runtime code comes from CPython 3.14, with minor changes to support older versions |
| 142 | of Python. For PyPy users, the C code has been ported to CFFI. |
| 143 | |
| 144 | During the build phase, the project uses [`zstd`](https://github.com/facebook/zstd) |
| 145 | (canonical implementation of Zstandard) as well as |
| 146 | [`pythoncapi-compat`](https://github.com/python/pythoncapi-compat) (which handles some |
| 147 | of the compatibility with older Python versions). |
| 148 | |
| 149 | Tests come from CPython 3.14, with minor changes to support older versions of Python. |
| 150 | Additional tests have been written specifically for `backports.zstd`. |
| 151 | |
| 152 | The type hints for the standard library have been contributed to |
| 153 | [`typeshed`](https://github.com/python/typeshed) and also backported to |
| 154 | `backports.zstd`. |
| 155 | |
| 156 | ### Why can this library not be installed with Python 3.14? |
| 157 | |
| 158 | This is [on purpose](https://github.com/Rogdham/backports.zstd/issues/50). For Python |
| 159 | 3.14 and later, use the `compression.zstd` module from the standard library. |
| 160 | |
| 161 | If you want your code to be compatible with multiple Python versions, condition the |
| 162 | usage of this library based on the Python version: |
| 163 | |
| 164 | - [During install](#install); |
| 165 | - [When importing at runtime](#usage). |
| 166 | |
| 167 | ### Can I use the libzstd version installed on my system? |
| 168 | |
| 169 | The wheels distributed on PyPI include a static version of `libzstd` for ease of |
| 170 | installation and reproducibility. |
| 171 | |
| 172 | If you want to use `libzstd` installed on your system, pass the `--system-zstd` argument |
| 173 | to the build backend. For example: |
| 174 | |
| 175 | ```sh |
| 176 | python -m pip install --config-settings=--build-option=--system-zstd ... |
| 177 | python -m build --wheel --config-setting=--build-option=--system-zstd ... |
| 178 | ``` |
| 179 | |
| 180 | If you run the test suite, set the environment variable |
| 181 | `BACKPORTSZSTD_SKIP_EXTENSION_TEST=1` to skip tests that may fail when using the system |
| 182 | library. |
| 183 | |
| 184 | ### I found a bug |
| 185 | |
| 186 | If you encounter any issues, please open a |
| 187 | [GitHub issue](https://github.com/Rogdham/backports.zstd/issues/new) with a minimal |
| 188 | reproducible example. |
| 189 | |
| 190 | We will check if the issue is with `backports.zstd` or CPython. We have already reported |
| 191 | and fixed a few issues in CPython this way! |