| | |
zipfile
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Aug 2009
Posts: 6
Reputation:
Solved Threads: 0
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
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
try
That definitely creates a zip file smaller than the original directory. (samples)
Python Syntax (Toggle Plain Text)
import zipfile import glob, os # open the zip file for writing, and write stuff to it file = zipfile.ZipFile("test.zip", "w") for name in glob.glob("samples/*"): file.write(name, os.path.basename(name), zipfile.ZIP_DEFLATED) file.close() # open the file again, to see what's in it file = zipfile.ZipFile("test.zip", "r") for info in file.infolist(): 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*
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?
What type of files are you trying to compress?
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)
# exploring Python file compression/decompression module bz2 # .bz2 files can be decompressed with WinRAR utility # uncompressed file 'longtext.txt' has size of 48 kbytes # level9 compressed 'longtext.bz2' has size of 1 kbyte # zip compressed file 'longtext.zip' is of size 95 kbytes! # tested with Python 3.1 import bz2 text = "use this to make one very long string right now\n" # create 1000 lines of repeating text text = text*1000 # create uncompressed text file uc_fname = 'longtext.txt' fout = open(uc_fname, 'w') fout.write(text) fout.close() # create bz2 compressed file (can be used with WinRAR) bz2_fname = 'longtext.bz2' bzout = bz2.BZ2File(bz2_fname, 'w', compresslevel=9) # Python3 requires text string to be encoded to bytearray bzout.write(text.encode('utf8')) bzout.close() # read bz2 compressed file bz2_fname = 'longtext.bz2' bzin = bz2.BZ2File(bz2_fname, 'r', compresslevel=9) # result of read() is of type string text2 = bzin.read() bzin.close() # short test ... print("Length of original text = %d" % len(text)) print("Length of recovered text = %d" % len(text2)) # the other way to compress ... # create zipfile compressed file # in this case the compressed file is larger than the original! import zipfile as zf z_fname = 'longtext.zip' zout = zf.ZipFile(z_fname, 'w', compression=zf.ZIP_DEFLATED) zout.write(uc_fname, text) zout.close()
Last edited by bumsfeld; Aug 24th, 2009 at 12:57 pm.
Should you find Irony, you can keep her!
![]() |
Similar Threads
- java.util.zip help (please) (Java)
- Zipfile (Python)
- video & sound freezing up (Windows NT / 2000 / XP)
- Error Message: Cannot find server; page cannot be displayed (Viruses, Spyware and other Nasties)
- Dialer in VB6 or Delphi that works on XP? (Visual Basic 4 / 5 / 6)
- zip file extraction (Java)
- Computer "freeze" on win. boot up (Windows NT / 2000 / XP)
Other Threads in the Python Forum
- Previous Thread: What is an API?
- Next Thread: Multimedia: introducing the webbrowser module
| Thread Tools | Search this Thread |
accessdenied apache application argv array beginner book builtin change color converter countpasswordentry curved dan08 dictionary dynamic edit enter examples file filename float format function gui homework import inches input java keyboard lapse library line lines linux list lists loop microphone mouse movingimageswithpygame mysql mysqlquery newb number numbers numeric output parameters parsing path phonebook plugin port prime programming projects py2exe pygame pyopengl pysimplewizard python random recursion redirect remote reverse scrolledtext session simple smtp software sprite statictext string strings syntax table tennis terminal text textarea thread threading time tkinter tlapse trick tuple tutorial ubuntu unicode unit urllib urllib2 variable windows wordgame wxpython






