zipstream-new


Namezipstream-new JSON
Version 1.1.8 PyPI version JSON
download
home_pagehttps://github.com/arjan-s/python-zipstream
SummaryZipfile generator that takes input files as well as streams
upload_time2020-09-14 09:26:44
maintainer
docs_urlNone
authorarjan5
requires_python
license
keywords zip streaming
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage No coveralls.
            
# python-zipstream

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:

```python
import zipstream

z = zipstream.ZipFile()
z.write('path/to/files')

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

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:

```python
def iterable():
    for _ in xrange(10):
        yield b'this is a byte string\x01\n'

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

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

Of course both approach can be combined:

```python
def iterable():
    for _ in xrange(10):
        yield b'this is a byte string\x01\n'

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

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

Since recent versions of web.py support returning iterators of strings to be
sent to the browser, to download a dynamically generated archive, you could
use something like this snippet:

```python
def GET(self):
    path = '/path/to/dir/of/files'
    zip_filename = 'files.zip'
    web.header('Content-type' , 'application/zip')
    web.header('Content-Disposition', 'attachment; filename="%s"' % (
        zip_filename,))
    return zipstream.ZipFile(path)
```

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

## Installation

```
pip install zipstream-new
```

## Requirements

  * Python 2.6+, 3.2+, pypy

## Examples

### flask

```python
from flask import Response

@app.route('/package.zip', methods=['GET'], endpoint='zipball')
def zipball():
    def generator():
        z = zipstream.ZipFile(mode='w', compression=zipstream.ZIP_DEFLATED)

        z.write('/path/to/file')

        for chunk in z:
            yield chunk

    response = Response(generator(), mimetype='application/zip')
    response.headers['Content-Disposition'] = 'attachment; filename={}'.format('files.zip')
    return response

# or

@app.route('/package.zip', methods=['GET'], endpoint='zipball')
def zipball():
    z = zipstream.ZipFile(mode='w', compression=zipstream.ZIP_DEFLATED)
    z.write('/path/to/file')

    response = Response(z, mimetype='application/zip')
    response.headers['Content-Disposition'] = 'attachment; filename={}'.format('files.zip')
    return response

# Partial flushing of the zip before closing

@app.route('/package.zip', methods=['GET'], endpoint='zipball')
def zipball():
    def generate_zip_with_manifest():
        z = zipstream.ZipFile(mode='w', compression=zipstream.ZIP_DEFLATED)

        manifest = []
        for filename in os.listdir('/path/to/files'):
            z.write(os.path.join('/path/to/files', filename), arcname=filename)
            yield from z.flush()
            manifest.append(filename)

        z.write_str('manifest.json', json.dumps(manifest).encode())

        yield from z

    response = Response(z, mimetype='application/zip')
    response.headers['Content-Disposition'] = 'attachment; filename={}'.format('files.zip')
    return response
```

### django 1.5+

```python
from django.http import StreamingHttpResponse

def zipball(request):
    z = zipstream.ZipFile(mode='w', compression=zipstream.ZIP_DEFLATED)
    z.write('/path/to/file')

    response = StreamingHttpResponse(z, content_type='application/zip')
    response['Content-Disposition'] = 'attachment; filename={}'.format('files.zip')
    return response
```

### webpy

```python
def GET(self):
    path = '/path/to/dir/of/files'
    zip_filename = 'files.zip'
    web.header('Content-type' , 'application/zip')
    web.header('Content-Disposition', 'attachment; filename="%s"' % (
        zip_filename,))
    return zipstream.ZipFile(path)
```

## Running tests

With python version > 2.6, just run the following command: `python -m unittest discover`

Alternatively, you can use `nose`.

If you want to run the tests on all supported Python versions, run `tox`.



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/arjan-s/python-zipstream",
    "name": "zipstream-new",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "zip streaming",
    "author": "arjan5",
    "author_email": "arjan@anymore.nl",
    "download_url": "https://files.pythonhosted.org/packages/e5/f3/1b5228576f215b200c7e922a280a92e4494df33baae6e0280a6f45371f13/zipstream-new-1.1.8.tar.gz",
    "platform": "",
    "description": "\n# python-zipstream\n\nzipstream.py is a zip archive generator based on python 3.3's zipfile.py. It was created to\ngenerate a zip file generator for streaming (ie web apps). This is beneficial for when you\nwant to provide a downloadable archive of a large collection of regular files, which would be infeasible to\ngenerate the archive prior to downloading or of a very large file that you do not want to store entirely on disk or on memory.\n\nThe archive is generated as an iterator of strings, which, when joined, form\nthe zip archive. For example, the following code snippet would write a zip\narchive containing files from 'path' to a normal file:\n\n```python\nimport zipstream\n\nz = zipstream.ZipFile()\nz.write('path/to/files')\n\nwith open('zipfile.zip', 'wb') as f:\n    for data in z:\n        f.write(data)\n```\n\nzipstream also allows to take as input a byte string iterable and to generate\nthe archive as an iterator.\nThis avoids storing large files on disk or in memory.\nTo do so you could use something like this snippet:\n\n```python\ndef iterable():\n    for _ in xrange(10):\n        yield b'this is a byte string\\x01\\n'\n\nz = zipstream.ZipFile()\nz.write_iter('my_archive_iter', iterable())\n\nwith open('zipfile.zip', 'wb') as f:\n    for data in z:\n        f.write(data)\n```\n\nOf course both approach can be combined:\n\n```python\ndef iterable():\n    for _ in xrange(10):\n        yield b'this is a byte string\\x01\\n'\n\nz = zipstream.ZipFile()\nz.write('path/to/files', 'my_archive_files')\nz.write_iter('my_archive_iter', iterable())\n\nwith open('zipfile.zip', 'wb') as f:\n    for data in z:\n        f.write(data)\n```\n\nSince recent versions of web.py support returning iterators of strings to be\nsent to the browser, to download a dynamically generated archive, you could\nuse something like this snippet:\n\n```python\ndef GET(self):\n    path = '/path/to/dir/of/files'\n    zip_filename = 'files.zip'\n    web.header('Content-type' , 'application/zip')\n    web.header('Content-Disposition', 'attachment; filename=\"%s\"' % (\n        zip_filename,))\n    return zipstream.ZipFile(path)\n```\n\nIf the zlib module is available, zipstream.ZipFile can generate compressed zip\narchives.\n\n## Installation\n\n```\npip install zipstream-new\n```\n\n## Requirements\n\n  * Python 2.6+, 3.2+, pypy\n\n## Examples\n\n### flask\n\n```python\nfrom flask import Response\n\n@app.route('/package.zip', methods=['GET'], endpoint='zipball')\ndef zipball():\n    def generator():\n        z = zipstream.ZipFile(mode='w', compression=zipstream.ZIP_DEFLATED)\n\n        z.write('/path/to/file')\n\n        for chunk in z:\n            yield chunk\n\n    response = Response(generator(), mimetype='application/zip')\n    response.headers['Content-Disposition'] = 'attachment; filename={}'.format('files.zip')\n    return response\n\n# or\n\n@app.route('/package.zip', methods=['GET'], endpoint='zipball')\ndef zipball():\n    z = zipstream.ZipFile(mode='w', compression=zipstream.ZIP_DEFLATED)\n    z.write('/path/to/file')\n\n    response = Response(z, mimetype='application/zip')\n    response.headers['Content-Disposition'] = 'attachment; filename={}'.format('files.zip')\n    return response\n\n# Partial flushing of the zip before closing\n\n@app.route('/package.zip', methods=['GET'], endpoint='zipball')\ndef zipball():\n    def generate_zip_with_manifest():\n        z = zipstream.ZipFile(mode='w', compression=zipstream.ZIP_DEFLATED)\n\n        manifest = []\n        for filename in os.listdir('/path/to/files'):\n            z.write(os.path.join('/path/to/files', filename), arcname=filename)\n            yield from z.flush()\n            manifest.append(filename)\n\n        z.write_str('manifest.json', json.dumps(manifest).encode())\n\n        yield from z\n\n    response = Response(z, mimetype='application/zip')\n    response.headers['Content-Disposition'] = 'attachment; filename={}'.format('files.zip')\n    return response\n```\n\n### django 1.5+\n\n```python\nfrom django.http import StreamingHttpResponse\n\ndef zipball(request):\n    z = zipstream.ZipFile(mode='w', compression=zipstream.ZIP_DEFLATED)\n    z.write('/path/to/file')\n\n    response = StreamingHttpResponse(z, content_type='application/zip')\n    response['Content-Disposition'] = 'attachment; filename={}'.format('files.zip')\n    return response\n```\n\n### webpy\n\n```python\ndef GET(self):\n    path = '/path/to/dir/of/files'\n    zip_filename = 'files.zip'\n    web.header('Content-type' , 'application/zip')\n    web.header('Content-Disposition', 'attachment; filename=\"%s\"' % (\n        zip_filename,))\n    return zipstream.ZipFile(path)\n```\n\n## Running tests\n\nWith python version > 2.6, just run the following command: `python -m unittest discover`\n\nAlternatively, you can use `nose`.\n\nIf you want to run the tests on all supported Python versions, run `tox`.\n\n\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "Zipfile generator that takes input files as well as streams",
    "version": "1.1.8",
    "split_keywords": [
        "zip",
        "streaming"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "5a50ff1a51e34a2161764455cb71f501",
                "sha256": "0662eb3ebe764fa168a5883cd8819ef83b94bd9e39955537188459d2264a7f60"
            },
            "downloads": -1,
            "filename": "zipstream_new-1.1.8-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "5a50ff1a51e34a2161764455cb71f501",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 20168,
            "upload_time": "2020-09-14T09:26:42",
            "upload_time_iso_8601": "2020-09-14T09:26:42.733531Z",
            "url": "https://files.pythonhosted.org/packages/81/f3/d7b4c8c9b6657ff0db27b739894ed0665fa8f3c78a7452bf74d6447f6865/zipstream_new-1.1.8-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "acd41cc94496e08bd8e86a83b214d9e0",
                "sha256": "b031fe181b94e51678389d26b174bc76382605a078d7d5d8f5beae083f111c76"
            },
            "downloads": -1,
            "filename": "zipstream-new-1.1.8.tar.gz",
            "has_sig": false,
            "md5_digest": "acd41cc94496e08bd8e86a83b214d9e0",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 9377,
            "upload_time": "2020-09-14T09:26:44",
            "upload_time_iso_8601": "2020-09-14T09:26:44.566776Z",
            "url": "https://files.pythonhosted.org/packages/e5/f3/1b5228576f215b200c7e922a280a92e4494df33baae6e0280a6f45371f13/zipstream-new-1.1.8.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2020-09-14 09:26:44",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "arjan-s",
    "github_project": "python-zipstream",
    "travis_ci": true,
    "coveralls": false,
    "github_actions": false,
    "tox": true,
    "lcname": "zipstream-new"
}
        
Elapsed time: 0.02062s