# Python compression libraries

Python compression libraries shrink data for storage or transfer and restore it byte for byte. Each codec trades compression ratio against speed, from fast codecs for streaming to slow codecs that maximize savings. These libraries let data pipelines, web servers, and file formats cut storage and bandwidth costs.

## Popularity

| Library | Monthly downloads | Stars | Last release | License |
| --- | --- | --- | --- | --- |
| [zstandard](#python-zstandard) | 194.6M | 649 | Sep 2025 | BSD-3-Clause |
| [Brotli](#brotli) | 123.5M | 15.1K | Oct 2026 | MIT |
| [LZ4](#python-lz) | 84.9M | 306 | Nov 2025 | BSD-3-Clause |
| [backports.zstd](#backports-zstd) | 59.1M | 31 | Oct 2026 | PSF-2.0 |
| [Zopfli](#zopfli) | 44.9M | 16 | Jun 2026 | Apache-2.0 |
| [cramjam](#cramjam) | 31.9M | 130 | Oct 2026 | MIT |
| [inflate64](#inflatesixfour) | 12.0M | 2 | Nov 2025 | LGPL-2.1-or-later |
| [PyPPMd](#pyppmd) | 11.6M | 12 | Nov 2025 | LGPL-2.1-or-later |

Downloads: PyPI, last thirty days. Stars and releases: GitHub. Amber: no release in over a year.

[python-zstandard](#python-zstandard) has the most monthly downloads in this comparison, at 188.2M.

## Capabilities

| Library | Multiple codec families | Incremental codec context |
| --- | --- | --- |
| [zstandard](#python-zstandard) | No | Yes |
| [Brotli](#brotli) | No | Yes |
| [LZ4](#python-lz) | No | Yes |
| [backports.zstd](#backports-zstd) | No | Yes |
| [Zopfli](#zopfli) | No | No |
| [cramjam](#cramjam) | Yes | Yes |
| [inflate64](#inflatesixfour) | No | Yes |
| [PyPPMd](#pyppmd) | No | Yes |

## Libraries

### zstandard

[GitHub](https://github.com/indygreg/python-zstandard) · [PyPI](https://pypi.org/project/zstandard)

Exposes Zstandard compression through one-shot and streaming Python APIs.

- Downloads / mo: 194.6M
- Stars: 649
- Last release: 0.25.0
- License: BSD-3-Clause
- Capabilities: [From the zstandard docs](https://github.com/indygreg/python-zstandard)

**Strengths**

- **Reuses compression contexts.** Reuse a compressor for repeated operations instead of recreating it.
- **Supports shared compression dictionaries.** Improve compression for small inputs that share recurring structure.

**Watch out for**

- **Concurrent operations need separate contexts.** Create a separate context for each simultaneously active operation.
- **One-shot operations hold both buffers.** Use streaming when input and output cannot fit in memory together.

```sh
python -m pip install zstandard
```

**dpkp/kafka-python** (5.9K stars)

[dpkp/kafka-python](https://github.com/dpkp/kafka-python) 

```python
def zstd_decode(payload):
    if not zstd:
        raise NotImplementedError("Zstd codec is not available")
    with zstd.ZstdDecompressor().stream_reader(io.BytesIO(payload), read_across_frames=True) as reader:
        return reader.read()

```

[kafka/codec.py](https://github.com/dpkp/kafka-python/blob/89df9871b84843ad8b9dbd48d5b8d11d06227acf/kafka/codec.py#L321-L325) · [More projects using zstandard →](https://dashboard.exa.ai/playground/search?q=Python%20projects%20using%20zstandard%20for%20framed%20compression%2C%20Kafka%20payloads%20and%20bounded%20stream%20processing&filters=%7B%22type%22%3A%20%22auto%22%2C%20%22numResults%22%3A%2010%2C%20%22includeDomains%22%3A%20%5B%22github.com%22%5D%7D)

**Quickstart**

```python
import zstandard as zstd

compressed = zstd.compress(b"hello")
print(compressed)

```

From [compressor.html](https://python-zstandard.readthedocs.io/en/latest/one_shot_api.html)

### Brotli

[GitHub](https://github.com/google/brotli) · [PyPI](https://pypi.org/project/brotli)

Binds the Brotli compression library for Python.

- Downloads / mo: 123.5M
- Stars: 15.1K
- Last release: 1.2.1
- License: MIT
- Capabilities: [From the Brotli docs](https://github.com/google/brotli)

```sh
python -m pip install brotli
```

**encode/httpx** (15.8K stars)

[encode/httpx](https://github.com/encode/httpx) 

```python
def decode(self, data: bytes) -> bytes:
        if not data:
            return b""
        self.seen_data = True
        try:
            return self._decompress(data)
        except brotli.error as exc:
            raise DecodingError(str(exc)) from exc

```

[httpx/_decoders.py](https://github.com/encode/httpx/blob/7cff5f79bc78649bbcd3fd9aa7a74c86e0f63016/httpx/_decoders.py#L136-L143) · [More projects using Brotli →](https://dashboard.exa.ai/playground/search?q=brotli%20GitHub%20projects)

**Quickstart**

```python
import brotli

compressed = brotli.compress(b"hello")
print(compressed)

```

From [brotli.py](https://github.com/google/brotli/blob/master/python/brotli.py)

### LZ4

[GitHub](https://github.com/python-lz4/python-lz4) · [PyPI](https://pypi.org/project/lz4)

Provides Python bindings for interoperable LZ4 frame compression.

- Downloads / mo: 84.9M
- Stars: 306
- Last release: 4.4.5
- License: BSD-3-Clause
- Capabilities: [From the LZ4 docs](https://github.com/python-lz4/python-lz4)

**Strengths**

- **Produces interoperable LZ4 frames.** Use frame bindings when exchanging compressed data with other implementations.
- **Processes data in chunks.** Compress and decompress incrementally instead of holding the full input.

**Watch out for**

- **Checksums are disabled by default.** Enable content_checksum when frame content needs checksum validation.
- **Buffered chunks can delay output.** Enable auto_flush when each compression call needs immediate output.

```sh
python -m pip install lz4
```

**joblib/joblib** (4.4K stars)

[joblib/joblib](https://github.com/joblib/joblib) 

```python
def compressor_file(self, fileobj, compresslevel=None):
        """Returns an instance of a compressor file object."""
        self._check_versions()
        if compresslevel is None:
            return self.fileobj_factory(fileobj, "wb")
        else:
            return self.fileobj_factory(fileobj, "wb", compression_level=compresslevel)

```

[joblib/compressor.py](https://github.com/joblib/joblib/blob/6a12020870c37029e605aa10739a4502445e390f/joblib/compressor.py#L209-L215) · [More projects using LZ4 →](https://dashboard.exa.ai/playground/search?q=Python%20projects%20using%20lz4%20for%20framed%20compression%2C%20Kafka%20payloads%20and%20bounded%20stream%20processing&filters=%7B%22type%22%3A%20%22auto%22%2C%20%22numResults%22%3A%2010%2C%20%22includeDomains%22%3A%20%5B%22github.com%22%5D%7D)

**Quickstart**

```python
import lz4.frame

compressed = lz4.frame.compress(b"hello")
print(compressed)

```

From [quickstart.rst](https://python-lz4.readthedocs.io/en/stable/lz4.frame.html)

### backports.zstd

[GitHub](https://github.com/Rogdham/backports.zstd) · [PyPI](https://pypi.org/project/backports.zstd)

Backports the compression.zstd module.

- Downloads / mo: 59.1M
- Stars: 31
- Last release: 1.8.0
- License: PSF-2.0
- Capabilities: [From the backports.zstd docs](https://github.com/Rogdham/backports.zstd)

```sh
python -m pip install "backports.zstd; python_version < '3.14'"
```

**urllib3/urllib3** (4.1K stars)

[urllib3/urllib3](https://github.com/urllib3/urllib3) 

```python
def decompress(self, data: bytes, max_length: int = -1) -> bytes:
            if not data and not self.has_unconsumed_tail:
                return b""
            if self._obj.eof:
                data = self._obj.unused_data + data
                self._obj = zstd.ZstdDecompressor()
            part = self._obj.decompress(data, max_length=max_length)
            length = len(part)
            data_parts = [part]
            # Every loop iteration is supposed to read data from a separate frame.
            # The loop breaks when:
            #   - enough data is read;
            #   - no more unused data is available;
            #   - end of the last read frame has not been reached (i.e.,
            #     more data has to be fed).
            while (
                self._obj.eof
                and self._obj.unused_data
                and (max_length < 0 or length < max_length)
            ):
                unused_data = self._obj.unused_data
                if not self._obj.needs_input:
                    self._obj = zstd.ZstdDecompressor()
                part = self._obj.decompress(
                    unused_data,
                    max_length=(max_length - length) if max_length > 0 else -1,
                )
                if part_length := len(part):
                    data_parts.append(part)
                    length += part_length
                elif self._obj.needs_input:
                    break
            return b"".join(data_parts)

```

[src/urllib3/response.py](https://github.com/urllib3/urllib3/blob/d9f9b0ad5b99247e06aa9a50b7a5747aa5c2e35d/src/urllib3/response.py#L286-L318) · [More projects using backports.zstd →](https://dashboard.exa.ai/playground/search?q=backports.zstd%20GitHub%20projects)

**Quickstart**

```python
import sys

if sys.version_info >= (3, 14):
    from compression import zstd
else:
    from backports import zstd

compressed = zstd.compress(b"hello")
print(compressed)

```

From [README.md](https://pypi.org/pypi/backports.zstd/1.8.0/json#info.description)

### Zopfli

[GitHub](https://github.com/fonttools/py-zopfli) · [PyPI](https://pypi.org/project/zopfli)

Provides the Zopfli compressor for Python.

- Downloads / mo: 44.9M
- Stars: 16
- Last release: 0.4.3
- License: Apache-2.0
- Capabilities: [From the Zopfli docs](https://github.com/fonttools/py-zopfli)

```sh
python -m pip install zopfli
```

**fonttools/fonttools** (5.3K stars)

[fonttools/fonttools](https://github.com/fonttools/fonttools) 

```python
def compress(data, level=ZLIB_COMPRESSION_LEVEL):
    """Compress 'data' to Zlib format. If 'USE_ZOPFLI' variable is True,
    zopfli is used instead of the zlib module.
    The compression 'level' must be between 0 and 9. 1 gives best speed,
    9 gives best compression (0 gives no compression at all).
    The default value is a compromise between speed and compression (6).
    """
    if not (0 <= level <= 9):
        raise ValueError("Bad compression level: %s" % level)
    if not USE_ZOPFLI or level == 0:
        from zlib import compress

        return compress(data, level)
    else:
        from zopfli.zlib import compress

        return compress(data, numiterations=ZOPFLI_LEVELS[level])

```

[Lib/fontTools/ttLib/sfnt.py](https://github.com/fonttools/fonttools/blob/f3f799e1b6c090096d7dc9a6ca22f26dd25a4c81/Lib/fontTools/ttLib/sfnt.py#L186-L202) · [More projects using Zopfli →](https://dashboard.exa.ai/playground/search?q=zopfli%20GitHub%20projects)

**Quickstart**

```python
from zopfli.zlib import compress

compressed = compress(b"hello")
print(compressed)

```

From [README.rst](https://pypi.org/pypi/zopfli/0.4.3/json#info.description)

### cramjam

[GitHub](https://github.com/milesgranger/cramjam) · [PyPI](https://pypi.org/project/cramjam)

Bundles multiple compression algorithms behind Python bindings.

- Downloads / mo: 31.9M
- Stars: 130
- Last release: 2.14.0
- License: MIT
- Capabilities: [From the cramjam docs](https://github.com/milesgranger/cramjam)

**Strengths**

- **Offers multiple codec families.** Choose Snappy, Brotli, LZ4, Zstandard, and other codecs through one package.
- **Writes into supplied buffers.** Use compress_into and decompress_into with existing output storage.

**Watch out for**

- **Experimental codecs require source builds.** Enable each experimental feature when building from source.
- **Finishing consumes the streaming compressor.** Create another compressor after calling finish.

```sh
python -m pip install cramjam
```

**Quickstart**

```python
import cramjam

compressed = bytes(cramjam.snappy.compress(b"hello"))
print(compressed)

```

From [README.md](https://github.com/milesgranger/cramjam/blob/v2.14.0/README.md) · [Find projects using cramjam →](https://dashboard.exa.ai/playground/search?q=Python%20projects%20using%20cramjam%20for%20framed%20compression%2C%20Kafka%20payloads%20and%20bounded%20stream%20processing&filters=%7B%22type%22%3A%20%22auto%22%2C%20%22numResults%22%3A%2010%2C%20%22includeDomains%22%3A%20%5B%22github.com%22%5D%7D)

### inflate64

[GitHub](https://github.com/miurahr/inflate64) · [PyPI](https://pypi.org/project/inflate64)

Compresses and decompresses Deflate64 data.

- Downloads / mo: 12.0M
- Stars: 2
- Last release: 1.0.4
- License: LGPL-2.1-or-later
- Capabilities: [From the inflate64 docs](https://github.com/miurahr/inflate64)

```sh
python -m pip install inflate64
```

**miurahr/py7zr** (561 stars)

[miurahr/py7zr](https://github.com/miurahr/py7zr) 

```python
def decompress(self, data: bytes | bytearray | memoryview, max_length: int = -1) -> bytes:
        if not self._enabled:
            raise UnsupportedCompressionMethodError(None, "deflate64 is disabled on pypy.")
        if len(data) == 0:
            if self.flushed:
                return b""
            else:
                self.flushed = True
                return self._decompressor.inflate(b"")
        return self._decompressor.inflate(data)

```

[py7zr/compressor.py](https://github.com/miurahr/py7zr/blob/eab11e95bd12dfb56ec31200c8b2992a3e1acc12/py7zr/compressor.py#L308-L317) · [More projects using inflate64 →](https://dashboard.exa.ai/playground/search?q=inflate64%20GitHub%20projects)

**Quickstart**

```python
import inflate64

compressor = inflate64.Deflater()
compressed = compressor.deflate(b"hello") + compressor.flush()
print(compressed)

```

From [README.rst](https://pypi.org/pypi/inflate64/1.0.4/json#info.description)

### PyPPMd

[GitHub](https://github.com/miurahr/pyppmd) · [PyPI](https://pypi.org/project/pyppmd)

Compresses and decompresses PPMd data.

- Downloads / mo: 11.6M
- Stars: 12
- Last release: 1.3.1
- License: LGPL-2.1-or-later
- Capabilities: [From the PyPPMd docs](https://github.com/miurahr/pyppmd)

```sh
python -m pip install pyppmd
```

**miurahr/py7zr** (561 stars)

[miurahr/py7zr](https://github.com/miurahr/py7zr) 

```python
def decompress(self, data: bytes | bytearray | memoryview, max_length=-1) -> bytes:
        if len(data) == 0 and self.decoder.needs_input:
            return self.decoder.decode(b"\0", max_length)
        return self.decoder.decode(data, max_length)

```

[py7zr/compressor.py](https://github.com/miurahr/py7zr/blob/eab11e95bd12dfb56ec31200c8b2992a3e1acc12/py7zr/compressor.py#L347-L350) · [More projects using PyPPMd →](https://dashboard.exa.ai/playground/search?q=pyppmd%20GitHub%20projects)

**Quickstart**

```python
import pyppmd

compressed = pyppmd.compress(b"hello")
print(compressed)

```

From [__init__.py](https://github.com/miurahr/pyppmd/blob/v1.3.1/src/pyppmd/__init__.py)

## FAQ

### Which Python compression library should I use?

[zstandard](#python-zstandard) suits general compression with a strong ratio at high speed. [LZ4](#python-lz) suits workloads that need the fastest compression and decompression. [Brotli](#brotli) suits web assets served to browsers.

### Brotli vs gzip: which should I use?

Brotli usually compresses text smaller than gzip at a similar decompression speed. Browsers accept both encodings for HTTP responses.

### Zstd vs gzip: which should I use?

Zstandard compresses faster than gzip at a similar or higher ratio and decompresses much faster. gzip remains the choice where only the standard library or older tools are available.

### Does Python include compression modules?

The standard library includes zlib, gzip, and lzma modules. Recent releases add a Zstandard module, and [backports.zstd](#backports-zstd) brings that module to older versions.

### What is cramjam?

[cramjam](#cramjam) bundles many compression algorithms, including Snappy, Brotli, and Zstandard, behind one set of Rust-backed bindings.

## How this page was built

Data checked Nov 20, 2026.

Reproduce it with the Exa API:

```json
{
  "endpoint": "/search",
  "body": {
    "query": "Best Python compression libraries zstandard lz4 python-snappy Cramjam code examples",
    "numResults": 5
  }
}
```

Docs: https://exa.ai/docs/reference/search
