Install & Compatibility
Where this runs
tested against v1.1.8 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.920 runs
installs and imports cleanly · install 0.0s · import 0.027s · 17.9MB
glibcpy 3.10–3.920 runs
installs and imports cleanly · install 1.5s · import 0.025s · 18MB
16MB installed
● package 16MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
ZipFile
✓ from zipstream import ZipFile
ZIP_DEFLATED
✓ from zipstream import ZIP_DEFLATED
Used for specifying DEFLATE compression.
This quickstart demonstrates how to create a streaming ZIP archive by adding files from disk, direct string/bytes content, and content from a generator. The `ZipFile` object itself is an iterator that yields chunks of the ZIP file, suitable for direct streaming to a response or other byte sink.
import zipstream
import os
def generate_zip_stream():
# Create a dummy directory and files for demonstration
if not os.path.exists('test_files'):
os.makedirs('test_files')
with open('test_files/file1.txt', 'w') as f:
f.write('This is file 1 content.')
with open('test_files/file2.txt', 'w') as f:
f.write('This is file 2 content. More data...')
# Initialize ZipFile for streaming
# Use allowZip64=True for archives larger than 4GB or 65,535 files
# Use compression=zipstream.ZIP_DEFLATED for compressed output
zs = zipstream.ZipFile(mode='w', compression=zipstream.ZIP_DEFLATED, allowZip64=True)
# Add files from a path
zs.write('test_files/file1.txt', arcname='archive/document1.txt')
# Add content from a string or bytes directly
zs.writestr('archive/dynamic_content.json', b'{"data": [1, 2, 3]}')
# Add content from an iterable (e.g., a generator)
def data_generator():
yield b"Line 1\n"
yield b"Line 2\n"
yield b"Line 3\n"
zs.write_iter('archive/generated_log.txt', data_generator())
# Iterate over the zipstream to get chunks of the ZIP file
# These chunks can be streamed directly to a HTTP response or cloud storage
for chunk in zs:
yield chunk
# Clean up dummy files/directory (optional)
os.remove('test_files/file1.txt')
os.remove('test_files/file2.txt')
os.rmdir('test_files')
# Example of consuming the stream to a local file
with open('output.zip', 'wb') as f:
for chunk in generate_zip_stream():
f.write(chunk)
print("ZIP file 'output.zip' created successfully.")
Errors
Common errors & fixes
KeyError: 'There is no item named <large_number> in the archive'
Attempting to use `zipstream.ZipFile` directly with functions like `boto3.client.upload_fileobj` that expect a complete file-like object (with methods like `seek`, `tell`, `read`) instead of an iterable of byte chunks. The `KeyError` indicates an internal issue when `boto3` tries to interpret the iterable as a standard file.
fixImplement a wrapper class around the `zipstream.ZipFile` iterator that provides the `read()` method and manages a buffer to simulate a file-like object for compatibility with APIs like `upload_fileobj`.
TypeError: 'zipstream.ZipFile' object is not readable
Attempting to call `.read()` or `.seek()` on a `zipstream.ZipFile` object, which is an iterable generator of byte chunks, not a standard file-like object that supports such methods.
fixIterate directly over the `zipstream.ZipFile` object to consume its byte chunks: `for chunk in my_zip_stream: ...`. If a file-like interface is strictly required, a custom adapter class is needed.
Upgrade
Version history
1.1.8latest on PyPI · released Sep 14, 2020
Audit
Dependencies
aiofilesoptionalRequired for asynchronous streaming capabilities (AioZipStream).