无法使用 python zipfile 库解压缩带有密码的 .zip 文件

Mah*_*man 5 python zipfile python-3.x

我使用 Gnome Archive Manager (Ubuntu OS) 创建了一个 zip 文件。我使用密码创建了 zip 文件,并尝试使用zipfilePython 库解压缩它:

import zipfile

file_name = '/home/mahmoud/Desktop/tester.zip'
pswd = 'pass'

with zipfile.ZipFile(file_name, 'r') as zf:
    zf.printdir()
    zf.extractall(path='/home/mahmoud/Desktop/testfolder', pwd = bytes(pswd, 'utf-8'))
Run Code Online (Sandbox Code Playgroud)

当我运行此代码时,出现以下错误,并且我非常确定密码正确。错误是:

File "/home/mahmoud/anaconda3/lib/python3.7/zipfile.py", line 1538, in open
  raise RuntimeError("Bad password for file %r" % name)

RuntimeError: Bad password for file <ZipInfo filename='NegSkew.pdf' compress_type=99 filemode='-rw-rw-r--' external_attr=0x8020 file_size=233252 compress_size=199427>
Run Code Online (Sandbox Code Playgroud)

如何解压缩文件?

小智 2

zipfile库不支持 AES 加密 (compress_type=99),仅支持代码中提到的 CRC-32 _ZipDecrypter( https://hg.python.org/cpython/file/a80c14ace927/Lib/zipfile.py#l508 )。_ZipDecrypter在 ZipFile.open 中引发特定 RuntimeError 之前调用并使用它,可以从extractall.

您可以使用pyzipper库(https://github.com/danifus/pyzipper)而不是zipfile解压缩文件:

import pyzipper

file_name = '/home/mahmoud/Desktop/tester.zip'
pswd = 'pass'

with pyzipper.AESZipFile(file_name) as zf:
    zf.extractall(path='/home/mahmoud/Desktop/testfolder', pwd = bytes(pswd, 'utf-8'))
Run Code Online (Sandbox Code Playgroud)