Compress Files with Zip (Python)
By
vegaseat on
Jan 19th, 2007 4:39 am 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
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
Mason_1
Newbie Poster
1 post since Feb 2008
Reputation Points: 10
Solved Threads: 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
aki.h
Newbie Poster
1 post since Apr 2010
Reputation Points: 10
Solved Threads: 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)
tudza
Newbie Poster
1 post since Aug 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
© 2013 DaniWeb® LLC
Page rendered in 0.0824 seconds
using 2.66MB