【发布时间】:2019-12-21 16:26:46
【问题描述】:
我需要创建一个临时 zip 文件来存储文件。该 ZIP 文件需要加密,因此 zipfile 不会在这里起到作用。该文件将被进一步加密(ZIP 将被再次加密到另一个文件中),因此压缩文件被用作减小其大小以加快互联网传输以及第一层加密的一种方式。 到目前为止,这是我所得到的:
import getpass
import tempfile
import pyminizip
def ZipFunction():
#This zips the file and encrypts it with a password
filename = input('Enter the file name: ')
passkey = getpass.getpass(prompt='Enter the password for the file: ')
passkey2 = getpass.getpass(prompt='Confirm the password: ')
if passkey != passkey2:
print('The passwords must be the same! Please restart the process.')
exit()
else:
#Here's where I need help
with tempfile.TemporaryFile() as tmp:
with pyminizip.compress(filename,None,tmp,passkey,9) as archive:
zipstring = archive.readlines()
#From here on the zipstring var is encrypted and creates the "further encrypted"
#file. From here on the script works normally
返回的错误是ValueError: expected arguments are compress(src, srcpath, dst, pass, level)。
我愿意将 pyminizip 更改为另一种可以创建加密 zip 文件的工具。这个“双重加密层”是客户的需求,虽然我真的觉得没有必要,但我没有权力从项目中废掉它。
我不习惯处理临时文件。我在这里做错了什么?
【问题讨论】:
-
看起来可疑的一件事是您将
None传递为srcpath。 -
谢谢马蒂诺!尽管这看起来很奇怪,但在此上下文之外使用的同一行创建了一个功能完善的 zip 文件。我尝试将
None更改为os.getcwd(),但无济于事。