在 Python 中创建一个加密的 ZIP 文件 [英] Create an encrypted ZIP file in Python

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

问题描述

我正在 Python 2.5 中使用 ZipFile 创建一个 ZIP 文件,到目前为止它工作正常:

I am creating an ZIP file with ZipFile in Python 2.5, it works ok so far:

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"的方式.我正在寻找开源解决方案.

but I couldn't find how to encrypt the files in the ZIP file. I could use system and call PKZIP -s, but I suppose there must be a more "Pythonic" way. I'm looking for an open source solution.

推荐答案

我创建了一个简单的库来在 python 中创建密码加密的 zip 文件.- 这里

I created a simple library to create a password encrypted zip file in python. - here

import pyminizip

compression_level = 5 # 1-9
pyminizip.compress("src.txt", "dst.zip", "password", compression_level)

该库需要 zlib.

我已经检查过该文件可以在 WINDOWS/MAC 中提取.

I have checked that the file can be extracted in WINDOWS/MAC.

这篇关于在 Python 中创建一个加密的 ZIP 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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