Zipstream-NG

1.9.0 · active · verified Wed Apr 15

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.

Warnings

Install

Imports

Quickstart

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.

from zipstream import ZipStream
import os

# Create a dummy directory and files for the example
if not os.path.exists('my_files'):
    os.makedirs('my_files')
with open('my_files/file1.txt', 'w') as f:
    f.write('This is file one.')
with open('my_files/file2.txt', 'w') as f:
    f.write('This is file two.')

# Stream files from a directory into a zip file
output_filename = 'archive.zip'
zs = ZipStream.from_path('my_files')

# To save to a local file:
with open(output_filename, 'wb') as f:
    for chunk in zs:
        f.write(chunk)

print(f'Successfully created {output_filename} containing files from my_files/')

# Clean up dummy files
os.remove('my_files/file1.txt')
os.remove('my_files/file2.txt')
os.rmdir('my_files')

view raw JSON →