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

Recommended Answers

All 3 Replies

Member Avatar for iamthwee

try

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)

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?

commented: Kudos right back at ya. +23

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:

# 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()
commented: Nice info +23
commented: Good one +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.