为什么python不能使用zip方法解压缩由winrar创建的受密码保护的zip文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25336859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
why can't python unzip a password protected zip file created by winrar using the zip method?
提问by wookie
I have searched the web high and low but still couldn't find a solution for the above problem. Does anyone out there know why and if so how it can be done?
我在网上搜索了高低,但仍然找不到上述问题的解决方案。有没有人知道为什么,如果知道,怎么做?
psw="dg"
ZipFile.extractall("data.zip", None, psw)
The error that I've got:
我得到的错误:
TypeError: unbound method extractall() must be called
with ZipFile instance as first argument (got str instance instead)
采纳答案by Bruno Gelb
Because you are using it wrong. :) From docs:
因为你用错了。:) 来自文档:
ZipFile.extractall([path[, members[, pwd]]])
Extract all members from the archive to the current working directory. path specifies a different directory to extract to. members is optional and must be a subset of the list returned by namelist(). pwd is the password used for encrypted files.
压缩文件。extractall([path[, members[, pwd]]])
将存档中的所有成员提取到当前工作目录。 path 指定要提取到的不同目录。members 是可选的,并且必须是 namelist() 返回的列表的子集。pwd 是用于加密文件的密码。
So you should call that this function for ZipFile object, not as static method. And you should not pass name of archive as a first argument. :)
所以你应该为 ZipFile 对象调用这个函数,而不是作为静态方法。并且您不应将存档名称作为第一个参数传递。:)
this way it'll work:
这样它就会起作用:
from zipfile import ZipFile
with ZipFile('data.zip') as zf:
zf.extractall(pwd='dg'
EDIT, in newer versions use:
编辑,在较新的版本中使用:
zf.extractall(pwd=b'dg')

