2

I have been trying to make a python script to zip a file with the zipfile module. Although the text file is made into a zip file, It doesn't seem to be compressing it; testtext.txt is 1024KB whilst testtext.zip (The code's creation) is also equal to 1024KB. However, if I compress testtext.txt manually in File Explorer, the resulting zip file is compressed (To 2KB, specifically). How, if possible, can I combat this logical error?

Below is the script that I have used to (unsuccessfully) zip a text file.

from zipfile import ZipFile

textFile = ZipFile("compressedtextstuff.zip", "w")
textFile.write("testtext.txt")
textFile.close()

3 Answers3

5

Well that's odd. Python's zipfile defaults to the stored compression method, which does not compress! (Why would they do that?)

You need to specify a compression method. Use ZIP_DEFLATED, which is the most widely supported.

import zipfile
zip = zipfile.ZipFile("stuff.zip", "w", zipfile.ZIP_DEFLATED)
zip.write("test.txt")
zip.close()
Mark Adler
  • 101,978
  • 13
  • 118
  • 158
  • Unfortunately the code you have specified doesn't work; it returns: Traceback (most recent call last): File "C:\Users\danal\Documents\pythonziptest\test.py", line 3, in zipFile = zipfile.ZipFile("comptesttext.zip", "w", zipfile.ZIP_DEFLATED) NameError: name 'zipfile' is not defined. Did you mean: 'ZipFile'? Edit: Never mind, I must have imported it incorrectly. The program now works as intended, thank you very much Mark! – Ihavenogoddamnclue Feb 26 '22 at 16:47
  • 1
    I tested that code, it worked fine, and then I copied and pasted it into the answer. You would need to post your code for us to see what you did wrong. – Mark Adler Feb 26 '22 at 17:51
  • You are right. I deleted it because the answer to my suggestion is already [answered in SO](https://stackoverflow.com/questions/27991745/zip-file-and-avoid-directory-structure/27992286#27992286) – abu Oct 06 '22 at 08:34
1

From the https://docs.python.org/3/library/zipfile.html#zipfile-objects it suggest example:

with ZipFile('spam.zip', 'w') as myzip:
myzip.write('eggs.txt')

So your code will be

from zipfile import ZipFile
 
with ZipFile('compressedtextstuff.zip', 'w', zipfile.ZIP_DEFLATED) as myzip:
myzip.write('testtext.txt')
tfv
  • 6,016
  • 4
  • 36
  • 67
Leo
  • 21
  • 2
-1

https://docs.python.org/3/library/zipfile.html#:~:text=with%20ZipFile(%27spam.zip%27%2C%20%27w%27)%20as%20myzip%3A%0A%20%20%20%20myzip.write(%27eggs.txt%27)

In the docs they have it written with a with statement so I would try that first.

Edit:

I just came back to say that you have to specify your compression method but Mark beat me to the punch.

Here is a link to a StackOverflow post about it https://stackoverflow.com/questions/4166447/python-zipfile-module-doesnt-seem-to-be-compressing-my-files#:~:text=This%20is%20because%20ZipFile%20requires,the%20method%20to%20be%20zipfile.

  • Unfortunately, when using the code provided, the outputted zip file is in fact 1025KB, which is larger than the original text file. Which is weird. – Ihavenogoddamnclue Feb 26 '22 at 15:45
  • is the text file randomly generated? – Austin Heard Feb 26 '22 at 15:50
  • kinda, the text file is just 1,000,000 zeros (for test purposes) – Ihavenogoddamnclue Feb 26 '22 at 15:55
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 26 '22 at 19:46