Zipstream-NG is a modern and easy-to-use Python library for generating streamable ZIP files on the fly. It allows packaging and streaming many files and folders without requiring temporary files or excessive memory, making it ideal for web backends. It also supports calculating the final ZIP file size before generation, enabling accurate Content-Length headers for HTTP responses. The current version is 1.9.0, and it maintains an active release cadence.
pip install zipstream-ngVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to create a ZipStream from a local directory path and then iterate over its chunks to write the streamed ZIP content to a new file. This pattern is easily adaptable for streaming over an HTTP response or other byte sinks.
Always use `from zipstream import ZipStream` after installing `zipstream-ng`. Verify the correct package is installed with `pip show zipstream-ng`.
For web applications, ensure you set `Content-Type: application/zip`, `Content-Disposition: attachment; filename="your_file.zip"`, and, if possible, `Content-Length` using `len(zs)` (where `zs` is your ZipStream instance without compression). Ensure the entire generator is consumed and sent to the client.
Always encode string data to bytes before adding it to `ZipStream`. For example: `zs.add(my_string.encode('utf-8'), 'text_file.txt')`.Install the package using pip: `pip install zipstream-ng`
Encode the string to bytes (e.g., UTF-8) before passing it as data: `zip_stream.add('file.txt', 'hello'.encode('utf-8'))`Use an `async for` loop to consume the asynchronous stream within an `async` function: `async for chunk in aio_zip_stream: ...`
Iterate over the `ZipStream` object to get the chunks of the ZIP file: `for chunk in zip_stream: ...` or `content = b''.join(zip_stream)`