创建受密码保护的zip文件Python [英] Create password protected zip file Python

查看:135
本文介绍了创建受密码保护的zip文件Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码在我的Python34应用程序中使用 zipFile 从用户上传的文件创建受密码保护的zip文件。但是,当我从Windows打开zip文件时,它不会要求输入密码。稍后,我将使用相同的密码从python读取zip文件。我在做什么错?

I'm using following code to create password protected zip file, from a file uploaded by user, in my Python34 application using zipFile. But when I open the zip file from windows, it doesn't ask for the password. I will be using the same password to read zip files from python later on. What am I doing wrong?

这是我的代码:

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")


推荐答案

zipfile 的文档表示 ZipFile .setpassword 设置默认密码以提取加密pted文件。

The documentation for zipfile indicates that ZipFile.setpassword sets the "default password to extract encrypted files."

在文档的最顶部:它支持对ZIP存档中的加密文件进行解密,但目前无法创建加密文件。

At the very top of the documentation: "It supports decryption of encrypted files in ZIP archives, but it currently cannot create an encrypted file."

编辑:
要创建受密码保护的ZIP文件,请尝试使用 pyminizip

这篇关于创建受密码保护的zip文件Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆