Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decompress zip file with password fails - bug in Python?

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.

like image 856
Rox Avatar asked Dec 03 '22 03:12

Rox


1 Answers

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.

like image 188
lenik Avatar answered Feb 11 '23 05:02

lenik