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.
ZipFileis a context manager, so you can use thewithstatement, as with opening files. It's more readable, and will handle closing correctly even when you get an exception."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)?