Package: zipstream-new
python-zipstreamzipstream.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:importzipstreamz=zipstream.ZipFile()z.write('path/to/files')withopen('zipfile.zip','wb')asf:fordatainz: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:defiterable():for_inxrange(10):yieldb'this is a byte string\x01\n'z=zipstream.ZipFile()z.write_iter('my_archive_iter',iterable())withopen('zipfile.zip','wb')asf:fordatainz:f.write(data)Of course both approach can be combined:defiterable():for_inxrange(10):yieldb'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())withopen('zipfile.zip','wb')asf:fordatainz: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:defGET(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,))returnzipstream.ZipFile(path)If the zlib module is available, zipstream.ZipFile can generate compressed zip
archives.Installationpip install zipstream-newRequirementsPython 2.6+, 3.2+, pypyExamplesflaskfromflaskimportResponse@app.route('/package.zip',methods=['GET'],endpoint='zipball')defzipball():defgenerator():z=zipstream.ZipFile(mode='w',compression=zipstream.ZIP_DEFLATED)z.write('/path/to/file')forchunkinz:yieldchunkresponse=Response(generator(),mimetype='application/zip')response.headers['Content-Disposition']='attachment; filename={}'.format('files.zip')returnresponse# or@app.route('/package.zip',methods=['GET'],endpoint='zipball')defzipball():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')returnresponse# Partial flushing of the zip before closing@app.route('/package.zip',methods=['GET'],endpoint='zipball')defzipball():defgenerate_zip_with_manifest():z=zipstream.ZipFile(mode='w',compression=zipstream.ZIP_DEFLATED)manifest=[]forfilenameinos.listdir('/path/to/files'):z.write(os.path.join('/path/to/files',filename),arcname=filename)yield fromz.flush()manifest.append(filename)z.write_str('manifest.json',json.dumps(manifest).encode())yield fromzresponse=Response(z,mimetype='application/zip')response.headers['Content-Disposition']='attachment; filename={}'.format('files.zip')returnresponsedjango 1.5+fromdjango.httpimportStreamingHttpResponsedefzipball(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')returnresponsewebpydefGET(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,))returnzipstream.ZipFile(path)Running testsWith python version > 2.6, just run the following command:python -m unittest discoverAlternatively, you can usenose.If you want to run the tests on all supported Python versions, runtox.