{"id":6950,"library":"zipstream-ng","title":"Zipstream-NG","description":"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.","status":"active","version":"1.9.0","language":"en","source_language":"en","source_url":"https://github.com/pR0Ps/zipstream-ng","tags":["zip","streaming","archive","web-backend","file-generation","no-temp-files"],"install":[{"cmd":"pip install zipstream-ng","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Requires Python 3.5.0 or higher.","package":"python","optional":false}],"imports":[{"note":"The package is installed as 'zipstream-ng' but the main class 'ZipStream' is imported directly from the 'zipstream' module. Importing from 'zipstream_ng' will fail, and there's an older, separate library called 'zipstream' that is not `zipstream-ng` and may lead to unexpected behavior if imported by mistake.","wrong":"from zipstream_ng import ZipStream","symbol":"ZipStream","correct":"from zipstream import ZipStream"}],"quickstart":{"code":"from zipstream import ZipStream\nimport os\n\n# Create a dummy directory and files for the example\nif not os.path.exists('my_files'):\n    os.makedirs('my_files')\nwith open('my_files/file1.txt', 'w') as f:\n    f.write('This is file one.')\nwith open('my_files/file2.txt', 'w') as f:\n    f.write('This is file two.')\n\n# Stream files from a directory into a zip file\noutput_filename = 'archive.zip'\nzs = ZipStream.from_path('my_files')\n\n# To save to a local file:\nwith open(output_filename, 'wb') as f:\n    for chunk in zs:\n        f.write(chunk)\n\nprint(f'Successfully created {output_filename} containing files from my_files/')\n\n# Clean up dummy files\nos.remove('my_files/file1.txt')\nos.remove('my_files/file2.txt')\nos.rmdir('my_files')","lang":"python","description":"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."},"warnings":[{"fix":"Always use `from zipstream import ZipStream` after installing `zipstream-ng`. Verify the correct package is installed with `pip show zipstream-ng`.","message":"Confusing Package Name vs. Import Path: The library is installed via `pip install zipstream-ng` but the primary class is imported as `from zipstream import ZipStream`. There is an older, unrelated `zipstream` package on PyPI which is no longer maintained. Ensure you are importing `ZipStream` from the correct module (`zipstream`) after installing `zipstream-ng`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"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.","message":"Potential for Corrupt Zips in Web Streaming: When streaming ZIPs over HTTP, incorrect handling of HTTP headers (e.g., `Content-Length`, `Content-Disposition`) or premature termination of the stream can lead to corrupt downloads. `zipstream-ng` can pre-calculate the size (if no compression is used), which is crucial for `Content-Length`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always encode string data to bytes before adding it to `ZipStream`. For example: `zs.add(my_string.encode('utf-8'), 'text_file.txt')`.","message":"Encoding of String Data: When adding string data directly to the ZipStream (rather than files), ensure it's explicitly encoded to bytes (e.g., `my_string.encode('utf-8')`). Passing unencoded strings or incorrectly encoded bytes can lead to corrupted file contents within the ZIP archive.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z","problems":[]}