943,806 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 879
  • Python RSS
Aug 24th, 2009
0

zipfile

Expand Post »
Hi,

I am using this zipfile.Zipfile module to zip around 600 MB of data. But after zipping, the size of the zipped folder is app. same.

file = zipfile.ZipFile(yesterday_date_time + ".zip","w")
os.chdir(rawFileLocation)
dirList = os.listdir(rawFileLocation)
for fnames in dirList:
file.write(fnames)
file.close()

If i try to use compression method as ZIP_DEFLATED, it raises an error.

Please suggest.

Regards
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rajivgupta1107 is offline Offline
6 posts
since Aug 2009
Aug 24th, 2009
0

Re: zipfile

try

Python Syntax (Toggle Plain Text)
  1. import zipfile
  2. import glob, os
  3.  
  4. # open the zip file for writing, and write stuff to it
  5.  
  6. file = zipfile.ZipFile("test.zip", "w")
  7.  
  8. for name in glob.glob("samples/*"):
  9. file.write(name, os.path.basename(name), zipfile.ZIP_DEFLATED)
  10.  
  11. file.close()
  12.  
  13. # open the file again, to see what's in it
  14.  
  15. file = zipfile.ZipFile("test.zip", "r")
  16. for info in file.infolist():
  17. print info.filename, info.date_time, info.file_size, info.compress_size

That definitely creates a zip file smaller than the original directory. (samples)
Last edited by iamthwee; Aug 24th, 2009 at 5:58 am.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Aug 24th, 2009
1

Re: zipfile

Also keep in mind that some file-types (like jpeg for example) won't compress very well because they're already compressed.
What type of files are you trying to compress?
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Aug 24th, 2009
2

Re: zipfile

niek_e is correct, successful compression depends on the source and the compression algorithm. Here is one example of large repeating text that the zip algorithm fails with, but bz2 compression works great:
python Syntax (Toggle Plain Text)
  1. # exploring Python file compression/decompression module bz2
  2. # .bz2 files can be decompressed with WinRAR utility
  3. # uncompressed file 'longtext.txt' has size of 48 kbytes
  4. # level9 compressed 'longtext.bz2' has size of 1 kbyte
  5. # zip compressed file 'longtext.zip' is of size 95 kbytes!
  6. # tested with Python 3.1
  7.  
  8. import bz2
  9.  
  10. text = "use this to make one very long string right now\n"
  11. # create 1000 lines of repeating text
  12. text = text*1000
  13.  
  14. # create uncompressed text file
  15. uc_fname = 'longtext.txt'
  16. fout = open(uc_fname, 'w')
  17. fout.write(text)
  18. fout.close()
  19.  
  20. # create bz2 compressed file (can be used with WinRAR)
  21. bz2_fname = 'longtext.bz2'
  22. bzout = bz2.BZ2File(bz2_fname, 'w', compresslevel=9)
  23. # Python3 requires text string to be encoded to bytearray
  24. bzout.write(text.encode('utf8'))
  25. bzout.close()
  26.  
  27. # read bz2 compressed file
  28. bz2_fname = 'longtext.bz2'
  29. bzin = bz2.BZ2File(bz2_fname, 'r', compresslevel=9)
  30. # result of read() is of type string
  31. text2 = bzin.read()
  32. bzin.close()
  33.  
  34. # short test ...
  35. print("Length of original text = %d" % len(text))
  36. print("Length of recovered text = %d" % len(text2))
  37.  
  38. # the other way to compress ...
  39. # create zipfile compressed file
  40. # in this case the compressed file is larger than the original!
  41. import zipfile as zf
  42. z_fname = 'longtext.zip'
  43. zout = zf.ZipFile(z_fname, 'w', compression=zf.ZIP_DEFLATED)
  44. zout.write(uc_fname, text)
  45. zout.close()
Last edited by bumsfeld; Aug 24th, 2009 at 12:57 pm.
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: What is an API?
Next Thread in Python Forum Timeline: 3.1.1 email problems





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC