4

I get a strange error in python. When I try to extract a password protected file using the zip module, I get an exception when trying to set "oy" as password. Everything else seems to work. A bug in ZipFile module?

import zipfile zip = zipfile.ZipFile("file.zip", "r") zip.setpassword("oy".encode('utf-8')) zip.extractall() #Above password "oy" generates the error here zip.close() 

This is the exception I get:

Traceback (most recent call last): File "unzip.py", line 4, in <module> zip.extractall() File "C:\Program Files\Python32\lib\zipfile.py", line 1002, in extrac l self.extract(zipinfo, path, pwd) File "C:\Program Files\Python32\lib\zipfile.py", line 990, in extract return self._extract_member(member, path, pwd) File "C:\Program Files\Python32\lib\zipfile.py", line 1035, in _extra member shutil.copyfileobj(source, target) File "C:\Program Files\Python32\lib\shutil.py", line 65, in copyfileo buf = fsrc.read(length) File "C:\Program Files\Python32\lib\zipfile.py", line 581, in read data = self.read1(n - len(buf)) File "C:\Program Files\Python32\lib\zipfile.py", line 633, in read1 max(n - len_readbuffer, self.MIN_READ_SIZE) zlib.error: Error -3 while decompressing: invalid block type 

If I use UTF-16 as encoding I get this error:

zlib.error: Error -3 while decompressing: invalid distance too far back 

EDIT I have now tested on a virtual Linux machine with following stuff:

  • Python version: 2.6.5
  • I created a password protected zip file with zip -e file.zip hello.txt

Now it seems the problem is something else. Now I can extract the zip file even if the password is wrong!

try: zip.setpassword("ks") # "ks" is wrong password but it still extracts the zip zip.extractall() except RuntimeException: print "wrong!" 

Sometimes I can extract the zip file with an incorrect password. The file (inside the zip file) is then extracted but when I try to open it the information seems to be corrupted/decrypted.

6
  • 3
    Just as a note, ZipFile is a context manager, so you can use the with statement, as with opening files. It's more readable, and will handle closing correctly even when you get an exception. Commented May 5, 2012 at 18:49
  • have you tried different encodings? utf16? Commented May 5, 2012 at 19:17
  • @user1320237: Yes, I have. Same problem. But then it is other passwords that doen´t work. Commented May 5, 2012 at 19:18
  • Works for me, password-encoding-independent (there are no utf-8 relevant special characters in "oy" anyway). What version of python/zipfile/zlib do you use, what operating system, how did you create the archive, can you reproduce the error using other archives (with same/other password)? Commented May 8, 2012 at 7:52
  • Have you tried decompressing the file using 7zip for example? Just to check whether the error is in the file or not. Commented May 10, 2012 at 11:26

3 Answers 3

8

If there's a problem with the password, usually you get the following exception:

RuntimeError: ('Bad password for file', <zipfile.ZipInfo object at 0xb76dec2c>) 

Since your exception complains about block type, most probably your .zip archive is corrupted, have you tried to unpack it with standalone unzip utility?

Or maybe you have used something funny, like 7zip to create it, which makes incompatible .zip archives.


You don't provide enough information (OS version? Python version? ZIP archive creator and contents? are there many files in those archives or single file in single archive? do all those files give same errors, or you can unpack some of them?), so here's quick Q&A section, which should help you to find and remedy the problem.

Q1. Is this a bug in Python?

A1. Unlikely.

Q2. What might cause this behaviour?

A2. Broken zip files, incompatible zip compressors -- since you don't tell anything, it's hard to point the the exact cause.

Q3. How to find the cause?

A3. Try to isolate the problem, find the file which gives you an error, try to use zip.testzip() and/or decompress that particular file with different unzip utility, share the results. Only you have access to the problematic files, so nobody can help you unless you try to do something yourself.

Q4. How to fix this?

A4. You cannot. Use different zip extractor, ZipFile won't work.

Sign up to request clarification or add additional context in comments.

8 Comments

It works if the password of the zip-file is something else, like "hello". Then I can uncompress it with the above python code without problems. So it has to be something else. Maybe a bug?
as long as you use ASCII characters for password, there's no point to do encode('utf-8') and the like, it gives you back same ASCII characters. BTW, could you please elaborate, which software do you use to create your .zip files?
I don´t know what software that created the files. I have a bunch of them at my work and I also have a list of all the passwords for each zip file and I would like to make a python script that extracts them all. But, as I said above, some passwords does not seem to work, even if they are ASCII or UTF-16 encoded.
If I use utf-16 as encoding, I get this error: zlib.error: Error -3 while decompressing: invalid distance too far back
added a few questions and answers, hope this helps.
|
6
+25

Try using the testzip() method to check the file's integrity before extracting files.

3 Comments

Return "None". Does that mean that the zip file is not corrupted?
Yes. If the file were corrupt, it would return the name of the file it cannot read. Can you try to unpack the problematic zipfile with a different unzip program like unzip or winzip? If that works OK, it would be reasonable to suspect a Python bug. But do upgrade to the latest version of Python before filing a bug report.
On further reflection, the actual error comes from the dynamically loaded zlib module, which (on unix at least) uses the system's shared libz library. So if this is a bug, it could be in a number of places.
3

It could be possibly a bug in zipfile, or a bug in your zip implementation. I noted that your line numbers do not match mine so I guess this is python 3.2 earlier than the current 3.2.3 release I have.

Now, as to your code, it does work for me on Python 3.2.3 on Linux. I suggest you update to the latest 3.2.x as there seem to be a number of bug fixes related to zipfile and zlib, including fixes for crashes.

2 Comments

I have tried with 3.2 and 3.1. Seems to be the same error. Look at my edit in my post above.
No, I mean install the latest python 3.2, that is 3.2.3

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.