在Python中创建加密的ZIP文件
时间:2020-03-05 18:40:57 来源:igfitidea点击:
我正在使用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()
但我找不到如何加密ZIP文件中的文件。
我可以使用system并调用PKZIP -s,但是我想必须有一种更" Pythonic"的方式。我正在寻找一种开源解决方案。
解决方案
回答
我们可以使用Chilkat库。它是商业性的,但是具有免费评估功能,而且看起来还不错。
这是我从这里得到的一个例子:
import chilkat # Demonstrates how to create a WinZip-compatible 128-bit AES strong encrypted zip zip = chilkat.CkZip() zip.UnlockComponent("anything for 30-day trial") zip.NewZip("strongEncrypted.zip") # Set the Encryption property = 4, which indicates WinZip compatible AES encryption. zip.put_Encryption(4) # The key length can be 128, 192, or 256. zip.put_EncryptKeyLength(128) zip.SetPassword("secret") zip.AppendFiles("exampleData/*",True) zip.WriteZip()