python allan_python-zipstream

Enczipstream

python-zipstream.png?branch=master

Original Authors and Weak algorithm warning

enczipstream is the merge between allanlei's python-zipstream and

devthat's zipencrypt allowing a user to stream password

protected archive (with the weak algorithm)

WEAK ALGORITHM IS NOT SECURE

Readme

enczipstream.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 enczipstream

z = enczipstream.ZipFile()

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

with open('zipfile.zip', 'wb') as f:

for data in z:

f.write(data)

enczipstream 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:

def iterable():

for _ in xrange(10):

yield b'this is a byte string\x01\n'

z = enczipstream.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:

def iterable():

for _ in xrange(10):

yield b'this is a byte string\x01\n'

z = enczipstream.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:

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 enczipstream.ZipFile(path)

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

archives.

Installation

pip install enczipstream

Requirements

Python 2.6, 2.7, 3.2, 3.3, pypy

Examples

flask

from flask import Response

@app.route('/package.zip', methods=['GET'], endpoint='zipball')

def zipball():

def generator():

z = enczipstream.ZipFile(mode='w', compression=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 = enczipstream.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+

from django.http import StreamingHttpResponse

def zipball(request):

z = enczipstream.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

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 enczipstream.ZipFile(path)

password example

def iterable():

for _ in xrange(10):

yield b'this is a byte string\x01\n'

z = enczipstream.ZipFile(mode='w',

compression=enczipstream.ZIP_DEFLATED,

pwd=b"correct horse battery staple")

z.write_iter('my_archive_iter', iterable())

Running tests

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

Alternatively, you can use nose.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值