Compress Files with Zip (Python)

Updated vegaseat 0 Tallied Votes 856 Views Share

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:
http://www.daniweb.com/code/snippet216860.html

# 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)
zout.close()

print '-'*40

# test if the file is a valid pkzip file
if zipfile.is_zipfile(zfilename):
    print "%s is a valid pkzip file" % zfilename
else:
    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:"
zfile.printdir()
zfile.close()

print '-'*40

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

# display the file's contents ...
print "Checking the contents of %s:" % zfilename
print strz
Mason_1 0 Newbie Poster

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?

Mason

vegaseat: thanks for catching that one, corrected it

aki.h 0 Newbie Poster

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

tudza 0 Newbie Poster

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)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.