excess.org

Ian Ward

Consulting
Boxkite Inc.
Software
Urwid 2014-02-09 (1.2.0)
Speedometer 2011-12-08 (2.8)

Writing
Moving to Python 3
2011-02-17

Presentations
Urwid Applications
2012-11-14
Urwid Intro
2012-01-22
Unfortunate Python
2011-12-19
Django 1.1
2009-05-16
Article Tags

Home

Ian Ward's email:
first name at this domain

wardi on OFTC, freenode and github

Locations of visitors to this page

mkzip.py 1.2

Purpose

mkzip.py creates zip files from files stored on the web server. It can also calculate the final output size before creating a zip file. It uses no intermediate storage.

mkzip.py is designed to create custom zip files for web sites that let users select multiple files to download at the same time.

Source Code

Current version is 1.2

Version 1.2 browse module / download module

Example

The following is an example of a cgi script that outputs a zip file containing two avi files, one mp3, and a static readme file:

#!/usr/bin/python
import sys
import mkzip

files = [ ('bloopers/cat.avi', 'cat - bloopers.avi'),
          ('bloopers/skiing.avi', 'skiing - bloopers.avi'),
          ('music/waa-waa.mp3', 'waa-waa.mp3'), 
          (None, 'README', "Files zipped by mkzip!\n"), ]

sys.stdout.write( "Content-type: application/x-zip-compressed\r\n" )
sys.stdout.write( "Content-length: %d\r\n\r\n" % mkzip.size_of_zip(files) )

mkzip.create_zip( sys.stdout, files )

The first entry in each of the “files” tuples is the actual file name to be included. The second entry is the name of the file as it will appear in the zip file. If the first entry is None then the third entry contains the contents of the file as a string.

size_of_zip() calculates the total zip file size by reading the size of the files on disk.

create_zip() generates a zip file without any intermediate files stored on disk.