|
| 1 | + |
| 2 | +# ZipStream |
| 3 | + |
| 4 | +zipstream.py is a zip archive generator based on zipfile.py. It was created to |
| 5 | +generate a zip file on-the-fly for download in a web.py (http://webpy.org/) |
| 6 | +application. This is beneficial for when you want to provide a downloadable |
| 7 | +archive of a large collection of regular files, which would be infeasible to |
| 8 | +generate the archive prior to downloading. |
| 9 | + |
| 10 | +The archive is generated as an iterator of strings, which, when joined, form |
| 11 | +the zip archive. For example, the following code snippet would write a zip |
| 12 | +archive containing files from 'path' to a normal file: |
| 13 | + |
| 14 | +```python |
| 15 | +zf = open('zipfile.zip', 'wb') |
| 16 | +for data in ZipStream(path): |
| 17 | + zf.write(data) |
| 18 | +zf.close() |
| 19 | +``` |
| 20 | + |
| 21 | +Since recent versions of web.py support returning iterators of strings to be |
| 22 | +sent to the browser, to download a dynamically generated archive, you could |
| 23 | +use something like this snippet: |
| 24 | + |
| 25 | +```python |
| 26 | +def GET(self): |
| 27 | + path = '/path/to/dir/of/files' |
| 28 | + zip_filename = 'files.zip' |
| 29 | + web.header('Content-type' , 'application/zip') |
| 30 | + web.header('Content-Disposition', 'attachment; filename="%s"' % ( |
| 31 | + zip_filename,)) |
| 32 | + return ZipStream(path) |
| 33 | +``` |
| 34 | + |
| 35 | +If the zlib module is available, ZipStream can generate compressed zip |
| 36 | +archives. |
| 37 | + |
| 38 | +## Requirements |
| 39 | + |
| 40 | + * Python >=2.6 |
| 41 | + |
| 42 | +## License |
| 43 | + |
| 44 | +This library was created by SpiderOak, Inc. and is released under the GPLv3. |
| 45 | +Copyright 2008-2013 SpiderOak Inc. |
| 46 | + |
0 commit comments