Stream ZIP

0.0.84 · active · verified Tue Apr 14

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.

Warnings

Install

Imports

Quickstart

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.

from datetime import datetime
from stat import S_IFREG
from stream_zip import ZIP_32, stream_zip, ZIP_64

def generate_file_content(data):
    yield data.encode('utf-8')

def member_files():
    modified_at = datetime.now()
    mode = S_IFREG | 0o600 # Regular file, owner read/write

    # Example 1: Small file using ZIP_32 (default)
    yield (
        'my-file-1.txt',
        modified_at,
        mode,
        ZIP_32,
        generate_file_content('This is some content for file 1.')
    )

    # Example 2: Potentially larger file or for explicit 64-bit support
    yield (
        'my-file-2.json',
        modified_at,
        mode,
        ZIP_64,
        generate_file_content('{"key": "value", "data": [1, 2, 3]}')
    )

    # Example 3: An empty directory
    yield (
        'my-directory/',
        modified_at,
        S_IFREG | 0o755, # Directory permissions
        ZIP_32,
        ()
    )

# Stream the ZIP file chunks
zipped_chunks = stream_zip(member_files())

# In a real application, you would send these chunks directly as an HTTP response
# or write them to a file. For this example, we'll print them.
# You might also use io.BytesIO to collect them into a single bytes object for testing.

# Example of consuming chunks (e.g., writing to a file)
# with open('output.zip', 'wb') as f:
#     for chunk in zipped_chunks:
#         f.write(chunk)

print("Generated ZIP chunks (first few bytes of each):")
for i, chunk in enumerate(zipped_chunks):
    print(f"Chunk {i}: {len(chunk)} bytes (starts with: {chunk[:20]})")
    if i > 5: # Limit output for demonstration
        print("...")
        break

view raw JSON →