Stream-zip is a Python library designed to construct ZIP archives on the fly without requiring the entire archive or its constituent files to be held in memory or on disk. This makes it particularly suitable for memory-constrained environments or generating ZIP files for streaming HTTP responses in web servers. It offers both synchronous and asynchronous interfaces and is currently at version 0.0.84, with frequent minor updates.
pip install stream-zipVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use `stream_zip` to create a ZIP archive containing multiple files and a directory. The `stream_zip` function takes an iterable of member file definitions, where each member file is a tuple specifying its name, modification time, mode, compression method (`ZIP_32` or `ZIP_64`), and an iterable of binary content chunks. The function returns an iterable that yields chunks of the resulting ZIP file.
Consult the `stream-zip` documentation or changelog for `v0.0.81` to update the `async_stream_zip` call with the corrected signature, specifically regarding parameters like `chunk_size`.
For files potentially exceeding 4GiB (compressed or uncompressed size, or total archive size), use `ZIP_64`. Otherwise, `ZIP_32` offers broader compatibility. Be mindful of these limits for individual files and the overall archive.
Avoid `NO_COMPRESSION_*` for large files if memory efficiency is critical. If uncompressed data must be included, consider if another streaming approach or a different file format is more suitable, or accept the memory footprint for these specific files.
Be aware of potential memory spikes if creating ZIPs with an extremely high count of member files, even if each individual file's content is streamed efficiently.