繁体   English   中英

为什么 python 无法解压缩由 winrar 使用 zip 方法创建的受密码保护的 zip 文件?

[英]why can't python unzip a password protected zip file created by winrar using the zip method?

我搜索了 web 高低,但仍然找不到上述问题的解决方案。 有没有人知道为什么,如果是这样,怎么做?

psw="dg"

ZipFile.extractall("data.zip", None, psw)

我遇到的错误:

TypeError: unbound method extractall() must be called
with ZipFile instance as first argument (got str instance instead)

因为你用错了。 :) 来自文档

压缩文件 extractall ([path[, members[, pwd]]])

将存档中的所有成员提取到当前工作目录。 path 指定要提取到的不同目录 members 是可选的,并且必须是 namelist() 返回的列表的子集。 pwd 是用于加密文件的密码。

所以你应该为 ZipFile 对象调用这个函数,而不是作为静态方法。 并且您不应将存档名称作为第一个参数传递。 :)

这样它就会起作用:

from zipfile import ZipFile

with ZipFile('data.zip') as zf:
    zf.extractall(pwd='dg'

编辑,在较新的版本中使用:

zf.extractall(pwd=b'dg')

要提供没有首字母缩略词的确切语法:

from zipfile import ZipFile

str_zipFile = 'FileZip.zip'
str_pwd= 'xxxx'

with ZipFile(str_zipFile) as zipObj:
  zipObj.extractall(pwd = bytes(str_pwd,'utf-8'))

有些文件不知道它们来自什么并且提取时间很长。

比如60秒没有解压或者时间过了,我想删除原文件取消操作。

import pyzipper

with pyzipper.AESZipFile('test.zip', 'r', compression=pyzipper.ZIP_LZMA, encryption=pyzipper.WZ_AES) as extracted_zip:
        extracted_zip.extractall(pwd=str.encode("@apkclub"))
        os.remove(test.zip)

暂无
暂无

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

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