Skip to main content

Asynchronous Zipfile generator that takes input files as well as streams

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

asynczipstream-1.0.1.tar.gz (22.3 kB view hashes)

Uploaded Source

Built Distribution

asynczipstream-1.0.1-py3-none-any.whl (20.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page