Following up from last week’s article on reading zip archives, we show you how you can create your own archives using Python.

If you missed the article last week on reading zip files, then I suggest you go back and give it a quick once over. We’ll be using the same files from that example: two text files called “file1.txt” and “file2.txt”.

If you just want a new zip file with your files in it, then it’s a matter of a few lines using the zipfile module. First, create the archive in write mode:


>>> import zipfile
>>> z = zipfile.ZipFile("test2.zip", "w")

Then start adding files:


>>> z.write("file1.txt")
>>> z.write("file2.txt")

That’s essentially it. The zipfile module handles all the details of opening and reading the files, compressing them and inserting them into the archive. Don’t believe me? Then let’s take a look. First we close the file, then reopen it in read mode.


>>> z.close()
>>> z = zipfile.ZipFile("test2.zip")
>>> z.printdir()
File Name Modified Size
file1.txt 2007-10-11 14:03:12 54
file2.txt 2007-10-11 14:03:32 37

The printdir() method of a zipfile works just like the function we wrote in the last article to print out the files in the archive. You can also check that the files contain the data we expect:


>>> print z.read("file1.txt")
File One Contents

"Testing, testing, one two three."

But what if we want to add files to an existing zip? It’s simple enough, we just need to open the file in append mode. Opening an existing zip file in write mode will erase the zip, so be careful when you’re using that mode.

Let’s say theres a “file3.txt” with the following contents:


File Three Contents

1234567890

We add it to the zip we just made (test2.zip) with the following code:


>>> import zipfile
>>> z = zipfile.ZipFile("test2.zip", "a")
>>> z.printdir()
File Name Modified Size
file1.txt 2007-10-11 14:03:12 54
file2.txt 2007-10-11 14:03:32 37
>>> z.write("file3.txt")
>>> z.printdir()
File Name Modified Size
file1.txt 2007-10-11 14:03:12 54
file2.txt 2007-10-11 14:03:32 37
file3.txt 2007-10-16 14:25:46 32
>>> z.close()

Finally, you can write a program to extract zip files to disk in just a few lines. The following Python program extracts all zip files given as arguments to the current directory.


from zipfile import *
import sys

for zipname in sys.argv[1:]:
z = ZipFile(zipname)
for filename in z.namelist():
outfile = file(filename, "w")
outfile.write(z.read(filename))
outfile.close()

In action, it looks like this:


$ ls
test2.zip unzip.py
$ python unzip.py test2.zip 
$ ls
file1.txt file2.txt file3.txt test2.zip unzip.py
$ cat file1.txt 
File One Contents

"Testing, testing, one two three."

Be careful, this simple program will only work if the zip archive has a flat file structure, i.e. it contains no nested directories. If it does, you’ll need to adjust the program to create the directories as it goes, but I’ll leave that extension up to you.

Subscribe to the Developer Insider Newsletter

From the hottest programming languages to commentary on the Linux OS, get the developer and open source news and tips you need to know. Delivered Tuesdays and Thursdays

Subscribe to the Developer Insider Newsletter

From the hottest programming languages to commentary on the Linux OS, get the developer and open source news and tips you need to know. Delivered Tuesdays and Thursdays