Ian Ward

CKAN contributor/tech lead
Urwid author
PyRF contributor
Speedometer author

Contributing to Open Source
IASA E-Summit, 2014-05-16
Urwid Applications
Urwid Intro
Unfortunate Python
Django 1.1

Moving to Python 3
Article Tags


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


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


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:

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.