Tah*_*bal 4 python django zipfile python-3.x
我正在使用以下代码在的Python34应用程序中根据用户上传的文件创建受密码保护的zip文件zipFile。但是,当我从Windows打开zip文件时,它不会要求输入密码。稍后,我将使用相同的密码从python读取zip文件。我究竟做错了什么?
这是我的代码:
pwdZipFilePath = uploadFilePath + "encryptedZipFiles/"
filePath = uploadFilePath
if not os.path.exists(pwdZipFilePath):
os.makedirs(pwdZipFilePath)
#save csv file to a path
fd, filePath = tempfile.mkstemp(suffix=source.name, dir=filePath)
with open(filePath, 'wb') as dest:
shutil.copyfileobj(source, dest)
#convert that csv to zip
fd, pwdZipFilePath = tempfile.mkstemp(suffix=source.name + ".zip", dir=pwdZipFilePath)
with zipfile.ZipFile(pwdZipFilePath, 'w') as myzip:
myzip.write(filePath)
myzip.setpassword(b"tipi")
Run Code Online (Sandbox Code Playgroud)
内置zipfile模块不支持写入密码加密文件(只能读取)。您可以使用pyminizip:
import pyminizip
pyminizip.compress("dummy.txt", "myzip.zip", "noneshallpass", compression_level)
Run Code Online (Sandbox Code Playgroud)
或者,如果您使用的是 Windows/msysgit,并且不知道格式:
import os
os.system('tar cz dummy.txt | openssl enc -aes-256-cbc -e -k noneshallpass > mypacked.enc')
os.remove('dummy.txt')
os.system('openssl enc -aes-256-cbc -d -k noneshallpass -in mypacked.enc | tar xz')
Run Code Online (Sandbox Code Playgroud)
在对文件zipfile表明,ZipFile.setpassword设置了“默认密码提取加密的文件。”
在文档的最顶部:“它支持对ZIP存档中的加密文件进行解密,但当前无法创建加密文件。”
编辑:要创建受密码保护的ZIP文件,请尝试使用pyminizip之类的包。
| 归档时间: |
|
| 查看次数: |
8227 次 |
| 最近记录: |