Stream Inflate
stream-inflate is a Python library, currently at version 0.0.43, designed to uncompress DEFLATE and DEFLATE64 streams. It is written in pure Python but is compiled with Cython for performance. The library maintains an active development status with frequent, small releases addressing new Python versions and bug fixes.
Warnings
- breaking Python 3.6 support was officially dropped in version 0.0.42. Users on Python 3.6 or older must upgrade their Python environment or remain on an older version of stream-inflate.
- gotcha Versions prior to 0.0.37 contained a bug that could lead to an infinite loop when decompressing certain DEFLATE streams. It is highly recommended to upgrade to version 0.0.37 or newer to avoid this issue.
- gotcha The `stream_inflate` function has two primary usage patterns. The quickstart demonstrates `stream_inflate(iterable_of_chunks)`, which returns a tuple `(uncompressed_iterator, get_end_index)`. An alternative, more advanced pattern shown in PyPI documentation is to call `stream_inflate()` without arguments to get a tuple of functions `(uncompressed_chunks_callable, is_done_callable, num_bytes_unconsumed_callable)` for fine-grained control over stream processing. Ensure you use the correct pattern for your needs.
Install
-
pip install stream-inflate
Imports
- stream_inflate
from stream_inflate import stream_inflate
- stream_inflate64
from stream_inflate import stream_inflate64
Quickstart
import os
from stream_inflate import stream_inflate
def get_compressed_data_example():
# This is a dummy example. In a real scenario, this would yield bytes
# from a DEFLATE-compressed source (e.g., network stream, file).
# For demonstration, we'll use a pre-compressed string.
# 'x\x9c\xcbH\xcd\xc9\xc9W(\xcf/\xcaIQ\x04\x00\x1a\x07\x04\x16' is DEFLATE for 'Hello, world!'
yield b'x\x9c\xcbH\xcd\xc9\xc9W(\xcf/\xcaIQ\x04\x00\x1a\x07\x04\x16'
# To uncompress a DEFLATE stream
uncompressed_chunks_iterator, _ = stream_inflate(get_compressed_data_example())
for chunk in uncompressed_chunks_iterator:
print(f"Received uncompressed chunk: {chunk.decode()}")
# Example for a real network stream (requires httpx, not a direct dependency of stream-inflate)
# import httpx
# def compressed_chunks_from_url():
# with httpx.stream('GET', 'https://www.example.com/my_deflate_file') as r:
# yield from r.iter_raw(chunk_size=65536)
#
# For uncompressed_chunk in stream_inflate(compressed_chunks_from_url()):
# print(uncompressed_chunk)