如何使用 Python 将文件压缩为受密码保护的存档
How to compress files as password protected archive using Python
有没有办法将 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()
Python支持extracting password protected zips:
zipfile.ZipFile('myarchive.zip').extractall(pwd='P4$$W0rd')
遗憾的是,它不支持创建它们。您可以调用 7zip 等外部工具,也可以使用 this zlib wrapper.
等第三方库
使用gpg for encryption。那将是归档和压缩数据的单独包装器。
有没有办法将 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()
Python支持extracting password protected zips:
zipfile.ZipFile('myarchive.zip').extractall(pwd='P4$$W0rd')
遗憾的是,它不支持创建它们。您可以调用 7zip 等外部工具,也可以使用 this zlib wrapper.
等第三方库使用gpg for encryption。那将是归档和压缩数据的单独包装器。