繁体   English   中英

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

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

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

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代码中提到的 CRC-32 ( https://hg.python.org/cpython/file/a80c14ace927/Lib/zipfile 。 . _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'))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM