Python - 密码保护 Zip 文件夹

Python - Password Protecting Zip folders

该脚本当前在当前工作目录中创建一个 zip 文件夹,并使用当前登录的用户文件填充它(例如 "Documents and Settings\Owner*"。但是,我想用密码保护该 zip 文件夹;我已经在这里查看了可行的回复,但它们要么是旧帖子,要么未经发布问题的人确认。

那么,我如何使用密码保护已在 python 中创建的 zip 文件?

我的当前代码;

import os, zipfile, getpass, sys

try:
    user= getpass.getuser()
    print " [*] Creating a zip-folder in current working directory...\r"
    zf = zipfile.ZipFile(user + ".zip", "w", zipfile.ZIP_DEFLATED)
    sys.__stdout__
    directory = "C:\Documents and Settings\Owner"
    print " [*] Created successfully..."
    print" [*] Attempting to copy files...\r"
    for dirname, subdirs, files in os.walk(directory):
        sys.stdout.write(" [*] Now copying files...\r")
        if "Local Settings" in files:
                continue
        zf.write(dirname)
        for filename in files:
            if "NTUSER" in filename:
                continue
            elif "ntuser" in filename:
                continue
            elif user + ".zip" in filename:
                continue
            elif "UsrClass" in filename:
                continue
            zf.write(os.path.join(dirname, filename))
    print ' [*] Completed copying files to zip-file...'
except IOError as e:
    print ' [-] ' + e
except KeyboardInterrupt:
    print ' [-] Cancelling current operation'
    sys.exit(0)
zf.close()

来自docs

It [this module] supports decryption of encrypted files in ZIP archives, but it currently cannot create an encrypted file

很遗憾,您无法使用 zipfile 模块加密 zip。