1,105,391 Community Members

Compress Files with Zip (Python)

Member Avatar
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]

Compress and decompress files using the popular pkzip format and the Python module 'zipfile'.

Concept Error! This code just does archiving into a zip file, no compression!

To compress and archive files it is best to use module tarfile, see the snippet at:

# write and read a zipped file (in PKZIP format) with Python
# the syntax is mildly different from normal file read/write
# tested with Python24    vegaseat    18jan2007

import zipfile

# set up a test string ...
str1 = """There is no ham in hamburger.
Neither apple nor pine are in pineapple.
Boxing rings are square.
Writers write, but fingers don't fing.
Overlook and oversee are opposites.
Slim chance and fat chance are the same.
A house can burn up as it burns down.
An alarm goes off by going on.
Fill in a form by filling it out.

# save the text as a PKZIP format .zip file
arc_name = "English101.txt"
zfilename = "English101.zip"
zout = zipfile.ZipFile(zfilename, "w")
zout.writestr(arc_name, str1)

print '-'*40

# test if the file is a valid pkzip file
if zipfile.is_zipfile(zfilename):
    print "%s is a valid pkzip file" % zfilename
    print "%s is not a valid pkzip file" % zfilename

print '-'*40

# open a zip file and show information
zfile = zipfile.ZipFile( zfilename, "r" )
print "Simple file information retrieval:"

print '-'*40

# read the zipped file back in ...
zin = zipfile.ZipFile(zfilename, "r")
strz = zin.read(arc_name)

# display the file's contents ...
print "Checking the contents of %s:" % zfilename
print strz
Member Avatar
Newbie Poster
1 post since Feb 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]

I like the code (thank you!), but I think you have a typo. Using zfilename for both the name of the .zip file and the name of the .txt file it's creating, will result in a text file with a .zip extension, making it look and work as though you have a .zip within a .zip.

Changing zout.writestr(zfilename, str1) to zout.writestr("zfilename.txt", str1) will solve the problem, resulting in a .txt file within the .zip, so your unzipper won't get confused :). See what I mean?


vegaseat: thanks for catching that one, corrected it

Member Avatar
Newbie Poster
1 post since Apr 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]

i have used above piece of code, but i found that total size of all file is equal to the size of resultant zip file. so no compression is done. Its just put all the files in a folder and say save it with an extension. May be i am wrong someway, but i checked it correctly. so please provide necessary information

Member Avatar
Newbie Poster
1 post since Aug 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]

For some reason, Python zipfile writing defaults to uncompressed. If you want to add files to a zip file and compress them you have to use the ZIP_DEFLATED option:

file.write(name, os.path.basename(name), zipfile.ZIP_DEFLATED)

Start New Discussion
Tags Related to this Article