{"id":7885,"library":"zipstream-new","title":"Zipstream-new","description":"Zipstream-new is a Python library that functions as a zip archive generator, taking input files and streams to create a ZIP file in small chunks. This is particularly useful for streaming large archives in web applications or to cloud storage without holding the entire file in memory or on disk. The current version is 1.1.8, and while the last PyPI release was in 2020, the project remains active on GitHub with a focus on stable generation.","status":"active","version":"1.1.8","language":"en","source_language":"en","source_url":"https://github.com/arjan-s/python-zipstream","tags":["zip","stream","archive","generator","files","http-streaming"],"install":[{"cmd":"pip install zipstream-new","lang":"bash","label":"Install with pip"}],"dependencies":[{"reason":"Required for asynchronous streaming capabilities (AioZipStream).","package":"aiofiles","optional":true}],"imports":[{"symbol":"ZipFile","correct":"from zipstream import ZipFile"},{"note":"Used for specifying DEFLATE compression.","symbol":"ZIP_DEFLATED","correct":"from zipstream import ZIP_DEFLATED"}],"quickstart":{"code":"import zipstream\nimport os\n\ndef generate_zip_stream():\n    # Create a dummy directory and files for demonstration\n    if not os.path.exists('test_files'):\n        os.makedirs('test_files')\n    with open('test_files/file1.txt', 'w') as f:\n        f.write('This is file 1 content.')\n    with open('test_files/file2.txt', 'w') as f:\n        f.write('This is file 2 content. More data...')\n\n    # Initialize ZipFile for streaming\n    # Use allowZip64=True for archives larger than 4GB or 65,535 files\n    # Use compression=zipstream.ZIP_DEFLATED for compressed output\n    zs = zipstream.ZipFile(mode='w', compression=zipstream.ZIP_DEFLATED, allowZip64=True)\n\n    # Add files from a path\n    zs.write('test_files/file1.txt', arcname='archive/document1.txt')\n\n    # Add content from a string or bytes directly\n    zs.writestr('archive/dynamic_content.json', b'{\"data\": [1, 2, 3]}')\n\n    # Add content from an iterable (e.g., a generator)\n    def data_generator():\n        yield b\"Line 1\\n\"\n        yield b\"Line 2\\n\"\n        yield b\"Line 3\\n\"\n    zs.write_iter('archive/generated_log.txt', data_generator())\n\n    # Iterate over the zipstream to get chunks of the ZIP file\n    # These chunks can be streamed directly to a HTTP response or cloud storage\n    for chunk in zs:\n        yield chunk\n\n    # Clean up dummy files/directory (optional)\n    os.remove('test_files/file1.txt')\n    os.remove('test_files/file2.txt')\n    os.rmdir('test_files')\n\n# Example of consuming the stream to a local file\nwith open('output.zip', 'wb') as f:\n    for chunk in generate_zip_stream():\n        f.write(chunk)\n\nprint(\"ZIP file 'output.zip' created successfully.\")","lang":"python","description":"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."},"warnings":[{"fix":"Initialize `ZipFile` with `allowZip64=True`, e.g., `zipstream.ZipFile(allowZip64=True)`.","message":"When streaming large archives (over 4GB or 65,535 files), `zipstream.ZipFile` defaults to the standard ZIP32 format, which has these limitations. For larger archives, you must explicitly enable ZIP64 extensions.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Wrap the `zipstream.ZipFile` iterator in a custom class that implements the file-like interface (e.g., `read()` method) to make it compatible.","message":"The `ZipFile` object is an iterator that yields byte chunks, not a traditional file-like object with `read()` or `seek()` methods. Direct use with APIs expecting a `file-like` object (e.g., `boto3.client.upload_fileobj`) will fail.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always provide the `arcname` argument for `zs.write(filename, arcname='desired/path/in/zip.txt')` to control the file's path within the archive.","message":"The `write` method with a single path argument (e.g., `z.write('path/to/files')`) implicitly adds files relative to the current working directory. For clarity and to avoid unexpected archive structures, it's recommended to always specify `arcname`.","severity":"deprecated","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Implement 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`.","cause":"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.","error":"KeyError: 'There is no item named <large_number> in the archive'"},{"fix":"Iterate 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.","cause":"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.","error":"TypeError: 'zipstream.ZipFile' object is not readable"}]}