Skip to content
Advertisement

Python zipfile module doesn’t compress files

I have a problem with compression in Python. I know I should call the ZIP_DEFLATED method when writing to make the zip file compressed, but it does not work for me. I have 3 PDF documents in the C:zip directory. When I run the following code it works just fine:

JavaScript

It makes the test.zip file without the compression. When I change the fourth row to this:

JavaScript

It also makes the test.zip file without the compression. I also tried to change the write method to give it the compress_ type argument:

JavaScript

But that doesn’t work either. I use Python version 2.7.4 with Win7. I tired the code with another computer (same circumstances, Win7 and Python 2.7.4), and it made the test.zip file compressed just like it should. I know the zlib module should be available, when I run this:

JavaScript

It doesn’t return an error, also if there would be something wrong with the zlib module the code at the top should had return an error too, so I suspect that zlib isn’t the problem.

Advertisement

Answer

By default the ZIP module only store data, to compress it you can do this:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement