Python で奇妙なエラーが発生します。zip モジュールを使用してパスワードで保護されたファイルを抽出しようとすると、「oy」をパスワードとして設定しようとすると例外が発生します。他のすべてはうまくいくようです。ZipFile モジュールのバグ?
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()
これは私が得る例外です:
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
エンコーディングとして UTF-16 を使用すると、次のエラーが発生します。
zlib.error: Error -3 while decompressing: invalid distance too far back
編集 次のものを使用して仮想Linuxマシンでテストしました:
- Python バージョン: 2.6.5
- パスワードで保護されたzipファイルを作成しました
zip -e file.zip hello.txt
今、問題は別のもののようです。パスワードが間違っていてもzipファイルを解凍できるようになりました!
try:
zip.setpassword("ks") # "ks" is wrong password but it still extracts the zip
zip.extractall()
except RuntimeException:
print "wrong!"
間違ったパスワードで zip ファイルを抽出できる場合があります。その後、ファイル (zip ファイル内) が抽出されますが、開こうとすると、情報が破損/復号化されているように見えます。