私はPythonで奇妙なエラーが発生します。 zipモジュールを使用してパスワードで保護されたファイルを抽出しようとすると、パスワードとして「oy」を設定しようとすると例外が発生します。他のすべてがうまくいくようです。 ZipFileモジュールのバグ?パスワードでZipファイルの解凍が失敗する - Pythonのバグ?
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
EDIT 次のものを持つ仮想Linuxマシン:
- Pythonのバージョン:2.6.5
- 私は今では問題は何か他のものであるようだ
zip -e file.zip hello.txt
でパスワード保護されたzipファイルを作成しました。パスワードが間違っていてもzipファイルを解凍できるようになりました!
try:
zip.setpassword("ks") # "ks" is wrong password but it still extracts the zip
zip.extractall()
except RuntimeException:
print "wrong!"
誤ったパスワードでzipファイルを抽出することがあります。ファイル(zipファイル内)が抽出されますが、開こうとすると情報が破損/復号化されたようです。
「ZipFile」はコンテキストマネージャなので、[with with文](http://docs.python.org/reference/compound_stmts.html#with)を使用することができますファイル。より読みやすく、例外が発生した場合でも正しく処理されます。 –
異なるエンコーディングを試しましたか? utf16? – User
@ user1320237:はい、あります。同じ問題。しかし、それはうまく動作しない他のパスワードです。 – Rox