Python - 密码保护Zip文件夹

时间:2015-04-18 15:04:21

标签: python-2.7 zipfile

该脚本当前在当前工作目录中创建一个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()

1 个答案:

答案 0 :(得分:2)

来自docs

  

[此模块]支持ZIP存档中加密文件的解密,但目前无法创建加密文件

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