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) 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