All Projects → allanlei → python-zipstream

allanlei / python-zipstream

Licence: GPL-3.0 License
Like Python's ZipFile module, except it works as a generator that provides the file in many small chunks.

Programming Languages

python
139335 projects - #7 most used programming language

Labels

Projects that are alternatives of or similar to python-zipstream

Zipstream Php
💾 PHP ZIP Streaming Library
Stars: ✭ 961 (+721.37%)
Mutual labels:  stream, zip
Iostreams
IOStreams is an incredibly powerful streaming library that makes changes to file formats, compression, encryption, or storage mechanism transparent to the application.
Stars: ✭ 84 (-28.21%)
Mutual labels:  stream, zip
Zipstorer
A Pure C# Class to Store Files in Zip
Stars: ✭ 139 (+18.8%)
Mutual labels:  stream, zip
secure-webrtc-swarm
💢 Create a swarm of p2p connections with invited peers using WebRTC.
Stars: ✭ 23 (-80.34%)
Mutual labels:  stream
prox
A Scala library for working with system processes
Stars: ✭ 93 (-20.51%)
Mutual labels:  stream
WebRTCCTV
WebRTCCTV is a signaling server & webapp able to stream from RTSP cameras using WebRTC
Stars: ✭ 32 (-72.65%)
Mutual labels:  stream
manifold-cljs
Manifold implementation in Clojurescript
Stars: ✭ 45 (-61.54%)
Mutual labels:  stream
ngx stream upstream check module
nginx health checker (tcp/udp/http) for stream upstream servers.
Stars: ✭ 18 (-84.62%)
Mutual labels:  stream
aspZip
A classic ASP zip and unzip utility class that uses the native zip support from Windows (XP and above) - no components needed
Stars: ✭ 24 (-79.49%)
Mutual labels:  zip
ToGoZip
Android share/sendTo menu implementation "add2Zip"
Stars: ✭ 44 (-62.39%)
Mutual labels:  zip
rc-zip
Pure rust zip & zip64 reading and writing
Stars: ✭ 93 (-20.51%)
Mutual labels:  zip
kinetic
High-Performance AWS Kinesis Client for Go
Stars: ✭ 20 (-82.91%)
Mutual labels:  stream
vidi
<video> playback simplified
Stars: ✭ 31 (-73.5%)
Mutual labels:  stream
gnip
Connect to Gnip streaming API and manage rules
Stars: ✭ 28 (-76.07%)
Mutual labels:  stream
TLightFileStream
Implements a lightweight, high-performance, non-allocating advanced-record-based wrapper around the SysUtils file handling routines as an alternative to Classes.TFileStream.
Stars: ✭ 21 (-82.05%)
Mutual labels:  stream
discord-ytdl-core
Simple ytdl wrapper for discord bots with custom ffmpeg args support.
Stars: ✭ 52 (-55.56%)
Mutual labels:  stream
MwK-Musics
A Telegram Bot to Play Audio in Voice Chats With Youtube and Deezer support. Supports Live streaming from youtube Supports Mega Radio Fm Streamings
Stars: ✭ 38 (-67.52%)
Mutual labels:  stream
vue-virtual-stream
Simple vue-virtualized package for Vue.js
Stars: ✭ 16 (-86.32%)
Mutual labels:  stream
sms
rtmp server and super media server whith golang.
Stars: ✭ 65 (-44.44%)
Mutual labels:  stream
playercast
Cast to media player and control playback remotely.
Stars: ✭ 46 (-60.68%)
Mutual labels:  stream

python-zipstream

Build Status Coverage Status

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:

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:

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:

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:

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

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 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=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 = 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

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].