Skip to main content

python-zipstream-aio

zipstream.py is a zip archive generator based on python 3.3's zipfile.py. It was created to generate a zip file generator for streaming (ie web apps). This is beneficial for when you want to provide a downloadable archive of a large collection of regular files, which would be infeasible to generate the archive prior to downloading or of a very large file that you do not want to store entirely on disk or on memory.

The archive is generated as an iterator of strings, which, when joined, form the zip archive. For example, the following code snippet would write a zip archive containing files from 'path' to a normal file:

import asynczipstream
import asyncio

async def main():
    z = asynczipstream.ZipFile()
    z.write('path/to/files')

    with open('zipfile.zip', 'wb') as f:
        async for data in z:
            f.write(data)

asyncio.run(main())

zipstream also allows to take as input a byte string iterable and to generate the archive as an iterator. This avoids storing large files on disk or in memory. To do so you could use something like this snippet:

import asynczipstream
import asyncio

async def main():
    async def iterable():
        for _ in range(10):
            yield b'this is a byte string\x01\n'

    z = asynczipstream.ZipFile()
    z.write_iter('my_archive_iter', iterable())

    with open('zipfile.zip', 'wb') as f:
        async for data in z:
            f.write(data)

asyncio.run(main())

Of course both approach can be combined:

import asynczipstream
import asyncio

async def main():
    async def iterable():
        for _ in range(10):
            yield b'this is a byte string\x01\n'

    z = asynczipstream.ZipFile()
    z.write('path/to/files', 'my_archive_files')
    z.write_iter('my_archive_iter', iterable())

    with open('zipfile.zip', 'wb') as f:
        async for data in z:
            f.write(data)

asyncio.run(main())

If the zlib module is available, asynczipstream.ZipFile can generate compressed zip archives.

Installation

pip install asynczipstream

Requirements

  • Python 3.8+

aiohttp Example

from aiohttp import web, hdrs
import asynczipstream

async def handle_license(request):
    """
        Example with file from disk
    """
    filename = "license.zip"
    response = web.StreamResponse(
        status=200,
        headers={
            hdrs.CONTENT_TYPE: "application/octet-stream",
            hdrs.CONTENT_DISPOSITION: (f"attachment; " f'filename="{filename}"; '),
        },
    )
    await response.prepare(request)

    z = asynczipstream.ZipFile()
    z.write('LICENSE')
    async for data in z:
        await response.write(data)
    return response

async def handle_readme(request):
    """
        Example with file from iterable object
    """
    filename = "readme.zip"
    response = web.StreamResponse(
        status=200,
        headers={
            hdrs.CONTENT_TYPE: "application/octet-stream",
            hdrs.CONTENT_DISPOSITION: (f"attachment; " f'filename="{filename}"; '),
        },
    )
    await response.prepare(request)

    async def iterable(filename):
        with open(filename, "rb") as f:
            yield f.readline()

    z = asynczipstream.ZipFile()
    z.write_iter('README.md', iterable("README.md"))
    async for data in z:
        await response.write(data)
    return response


app = web.Application()
app.add_routes([web.get('/license', handle_license),web.get('/readme', handle_readme)])

if __name__ == '__main__':
    web.run_app(app)

Running tests

Just run the following command: python -m unittest discover

Release files for asynczipstream 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for asynczipstream 1.0.1
File Size Uploaded
asynczipstream-1.0.1.tar.gz 22.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for asynczipstream 1.0.1
File Interpreter ABI Platform
asynczipstream-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 42.5 kB

Release files / asynczipstream-1.0.1.tar.gz

Download URL asynczipstream-1.0.1.tar.gz
Size 22.3 kB
Tags Source
SHA-256 checksum
How to use checksums
800fa0d9849e8ea46dcac8f283b0efe70d86e0f5a7c441c1896a068b94c3c2ed
BLAKE2b-256 checksum
How to use checksums
dfc43d094bc654226c0a7eac6c519d75e819e8743458a8e7dbb4b1d00d932f4e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.5

Release files / asynczipstream-1.0.1-py3-none-any.whl

Download URL asynczipstream-1.0.1-py3-none-any.whl
Size 20.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a57f78fb36054450f57472f3f10b3b15ba93cd2547a0055a1e7b08a78f82674d
BLAKE2b-256 checksum
How to use checksums
12ae83f33d0c5e02d223115ceeea4c9f4041b57000ba2f0815da692a7464d75c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.5

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page