【发布时间】:2018-05-12 20:31:57
【问题描述】:
我正在使用以下代码在我的 Python34 应用程序中使用 zipFile 从用户上传的文件创建受密码保护的 zip 文件。但是当我从 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")
【问题讨论】:
-
你真的可以访问文件文件吗?还是只是将其可视化?这是 zip 密码保护中的常见行为,即使文件受密码保护,您实际上也可以看到文件。
-
@user1767754 是的,我也可以访问 zip 中的文件。我什至打开了它。如果它仍然要打开它,设置密码有什么意义?
-
@Galen 我不这么认为。我已经检查了这个网站上的所有相关问题,但仍然有问题。
标签: python django python-3.x zipfile