Discussion:
Can't get compression in Python zip script.
Fredrik Lundh
2005-05-02 20:28:37 UTC
Permalink
I saw compression=0 and assumed that 1 would mean true, but no!
zipfile.ZIP_DEFLATED = 8, defies logic, it is the only compression type
why not accept any non zero as compression.
"is the only compression type" ? quoting from the ZIP specification:

compression method: (2 bytes)

(see accompanying documentation for algorithm
descriptions)

0 - The file is stored (no compression)
1 - The file is Shrunk
2 - The file is Reduced with compression factor 1
3 - The file is Reduced with compression factor 2
4 - The file is Reduced with compression factor 3
5 - The file is Reduced with compression factor 4
6 - The file is Imploded
7 - Reserved for Tokenizing compression algorithm
8 - The file is Deflated
9 - Enhanced Deflating using Deflate64(tm)
10 - PKWARE Data Compression Library Imploding
11 - Reserved by PKWARE
12 - File is compressed using BZIP2 algorithm

( http://www.pkware.com/company/standards/appnote/appnote.txt )

the current zipfile version only supports 0 and 8, but that doesn't mean that
future versions won't support other ZIP compressions.

</F>
M.E.Farmer
2005-05-02 14:29:51 UTC
Permalink
zipfile.ZipFile.__init__(self, filename, mode='r', compression=0)
Open the ZIP file with mode read "r", write "w" or append "a".
import zipfile
zip = zipfile.ZipFile('<path/zipfilename.zip>', 'w', compression=1)
zip.write('<path/filename.txt>')
zip.close()
Warning about zipfile module:
In Python2.2 this is true, maybe 2.3 and 2.4 also have some problems.
You cannot remove/delete an entry from a zipfile with Python but you
can read and write it!
To do it you will have to make a new zip file that contains all the
files you want to keep and then overwrite the original.
You can have mulitple files with the same name!
Obviusly that can cause problems.
This can be very confusing.
hth,
M.E.Farmer
M.E.Farmer
2005-05-02 21:00:44 UTC
Permalink
Thank you!
"""ZIP_DEFLATED
The numeric constant for the usual ZIP compression method. This
requires the zlib module. No other compression methods are currently
supported."""
That is where I got the 'only compression type'.
It all makes sense now.
Thanks again,
M.E.Farmer
deejaay
2005-05-02 13:47:13 UTC
Permalink
Hello,
I following an example on how to zip files from the www.devshed.com
site.
I can create a zip package, however I am not getting any compression.
120M file stays 120M within the zip package.
I need to know how to set or activate the compression values.
import zipfile
zip = zipfile.ZipFile('<path/zipfilename.zip>', 'w')
zip.write('<path/filename.txt>')
zip.close()
Any help would be gratly appreciated

deej
M.E.Farmer
2005-05-02 20:17:26 UTC
Permalink
Post by deejaay
Hello,
I following an example on how to zip files from the www.devshed.com
site.
I can create a zip package, however I am not getting any
compression.
Post by deejaay
120M file stays 120M within the zip package.
I need to know how to set or activate the compression values.
import zipfile
zip = zipfile.ZipFile('<path/zipfilename.zip>', 'w')
zip.write('<path/filename.txt>')
zip.close()
Any help would be gratly appreciated
It's not that difficult. The zipfile module uses null compression by
deafult, so you need to change the second line to read
zip = zipfile.ZipFile('<path/zipfilename.zip>', 'w',
zipfile.ZIP_DEFLATED)
That will create a comrpessed zip file.
regards
Steve
--
Steve Holden +1 703 861 4237 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/
Ooops! Never tried it at the interpreter, I should have, sorry.
Glad to see you got my back Steve;)
I should have just pointed here:
http://docs.python.org/lib/module-zipfile.html
Post by deejaay
a = zipfile.ZipFile('c:\tmp\z.zip', 'w', compression=1)
Traceback (most recent call last):
File "<input>", line 1, in ?
File "C:\Python22\Lib\zipfile.py", line 169, in __init__
raise RuntimeError, "That compression method is not supported"
RuntimeError: That compression method is not supported
Post by deejaay
a = zipfile.ZipFile('c:\tmp\z.zip', 'w', compression=8)
I saw compression=0 and assumed that 1 would mean true, but no!
zipfile.ZIP_DEFLATED = 8, defies logic, it is the only compression type
why not accept any non zero as compression.
Got any answers to the problems I outlined or have they been fixed yet
?
M.E.Farmer
M.E.Farmer
2005-05-02 23:01:26 UTC
Permalink
Yea I get it now. I didn't realize that there where other possible
un-implemented compression methods. Fredrik's post enlightened me.
The problem was a false assumption that compression=0 in the arguments
was == False seemed logical that compression=1 was True.
Sometimes things aren't logical till you see the full issue.
In plain english: I couldn't refuse the temptation to guess( and
guessed wrong ).
+1 on the bzip2
Thanks,
M.E.Farmer
Scott David Daniels
2005-05-02 22:08:41 UTC
Permalink
Post by Fredrik Lundh
I saw compression=0 and assumed that 1 would mean true, but no!
zipfile.ZIP_DEFLATED = 8, defies logic, it is the only compression type
why not accept any non zero as compression.
compression method: (2 bytes)
(see accompanying documentation for algorithm descriptions)
0 - The file is stored (no compression)
1 - The file is Shrunk
...
Post by Fredrik Lundh
8 - The file is Deflated
9 - Enhanced Deflating using Deflate64(tm)
10 - PKWARE Data Compression Library Imploding
11 - Reserved by PKWARE
12 - File is compressed using BZIP2 algorithm
( http://www.pkware.com/company/standards/appnote/appnote.txt )
the current zipfile version only supports 0 and 8, but that doesn't mean that
future versions won't support other ZIP compressions.
For example, I am hoping I'll get method 12 into Python 2.5. Please
don't use the unadorned numbers, however. Use zipfile.ZIP_STORE,
zipfile.ZIP_DEFLATED, and (later) zipfile.ZIP_BZIP2. This way you can
catch an exception when using an unimplemented compression format as
you retrieve the compression code, rather than waiting to get into the
midst of compression / decompression before getting a failure.

--Scott David Daniels
Scott.Daniels at Acm.Org

Steve Holden
2005-05-02 14:51:00 UTC
Permalink
Post by deejaay
Hello,
I following an example on how to zip files from the www.devshed.com
site.
I can create a zip package, however I am not getting any compression.
120M file stays 120M within the zip package.
I need to know how to set or activate the compression values.
import zipfile
zip = zipfile.ZipFile('<path/zipfilename.zip>', 'w')
zip.write('<path/filename.txt>')
zip.close()
Any help would be gratly appreciated
It's not that difficult. The zipfile module uses null compression by
deafult, so you need to change the second line to read

zip = zipfile.ZipFile('<path/zipfilename.zip>', 'w', zipfile.ZIP_DEFLATED)

That will create a comrpessed zip file.

regards
Steve
--
Steve Holden +1 703 861 4237 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/
Loading...