zipfile

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2009
Posts: 6
Reputation: rajivgupta1107 is an unknown quantity at this point 
Solved Threads: 0
rajivgupta1107 rajivgupta1107 is offline Offline
Newbie Poster

zipfile

 
0
  #1
Aug 24th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: zipfile

 
0
  #2
Aug 24th, 2009
try

  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.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,902
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 302
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: zipfile

 
1
  #3
Aug 24th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 137
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Re: zipfile

 
2
  #4
Aug 24th, 2009
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:
  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.
Should you find Irony, you can keep her!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC