我在Python 2.5中使用ZipFile创建一个ZIP文件,它到目前为止工作正常:
import zipfile, os
locfile = "test.txt"
loczip = os.path.splitext (locfile)[0] + ".zip"
zip = zipfile.ZipFile (loczip, "w")
zip.write (locfile)
zip.close()
Run Code Online (Sandbox Code Playgroud)
但我找不到如何加密ZIP文件中的文件.我可以使用系统并调用PKZIP -s,但我想必须有更"Pythonic"的方式.我正在寻找一个开源解决方案.
有没有办法压缩gzip或zipfile作为密码保护的存档?以下是示例代码,说明如何在没有密码保护的情况下归档文件:
import gzip, shutil
filepath = r'c:\my.log'
with gzip.GzipFile(filepath + ".gz", "w") as gz:
with open(filepath) as with_open_file:
shutil.copyfileobj(with_open_file, gz)
import zipfile
zf = zipfile.ZipFile(filepath + '.zip', 'w')
zf.write(filepath)
zf.close()
Run Code Online (Sandbox Code Playgroud) 我正在使用以下代码在的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) 我在(sav.zip)中有一个zip文件,我正在尝试为其设置密码:
zf = zipfile.ZipFile(“ sav.zip”)
zf.setpassword(“ 1234”)
但是...我收到TypeError:预期的字节数,得到了str
我的错在哪里