Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/allanlei/python-zipstream
Like Python's ZipFile module, except it works as a generator that provides the file in many small chunks.
https://github.com/allanlei/python-zipstream
python stream zip
Last synced: 30 days ago
JSON representation
Like Python's ZipFile module, except it works as a generator that provides the file in many small chunks.
- Host: GitHub
- URL: https://github.com/allanlei/python-zipstream
- Owner: allanlei
- License: gpl-3.0
- Fork: true (SpiderOak/ZipStream)
- Created: 2013-11-07T02:48:35.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2023-04-25T11:41:52.000Z (over 1 year ago)
- Last Synced: 2024-05-29T12:04:56.442Z (7 months ago)
- Topics: python, stream, zip
- Language: Python
- Size: 48.8 KB
- Stars: 128
- Watchers: 7
- Forks: 33
- Open Issues: 13
-
Metadata Files:
- Readme: README.markdown
- License: LICENSE
Awesome Lists containing this project
README
# python-zipstream
[![Build Status](https://travis-ci.org/allanlei/python-zipstream.png?branch=master)](https://travis-ci.org/allanlei/python-zipstream)
[![Coverage Status](https://coveralls.io/repos/allanlei/python-zipstream/badge.png)](https://coveralls.io/r/allanlei/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 zipstreamz = 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
```## Requirements
* Python 2.6, 2.7, 3.2, 3.3, 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=ZIP_DEFLATED)z.write('/path/to/file')
for chunk in z:
yield chunkresponse = 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=ZIP_DEFLATED)
z.write('/path/to/file')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 StreamingHttpResponsedef zipball(request):
z = zipstream.ZipFile(mode='w', compression=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`.