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

Unable to unzip a .zip file with a password with python zipfile library

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

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'))

当我 运行 此代码时,出现以下错误,我很确定密码是正确的。错误是:

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>

如何解压缩文件?

zipfile 库不支持 AES 加密 (compress_type=99),仅支持 _ZipDecrypter 代码 (https://hg.python.org/cpython/file/a80c14ace927/Lib/zipfile.py#l508) 中提到的 CRC-32。 _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'))