0

The script currently creates a zip folder at the current working directory, and populates it with the currently signed-in user files (e.g. "Documents and Settings\Owner*". However, I'm wanting to password protect the zip folder; I've checked on here for viable responses but they're either old posts, or were unconfirmed by the person posting the question.

So, How can I password protect a zip file thats already created in python?

My Current code;

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()
Luke Willmer
  • 327
  • 2
  • 6
  • 14

1 Answers1

3

From the docs:

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

So unfortunately you can't encrypt a zip using the zipfile module.

mata
  • 67,110
  • 10
  • 163
  • 162
  • Ah okay thanks. What would you recommend to encrypt a zip folder on Windows XP using Python? – Luke Willmer Apr 19 '15 at 15:49
  • 1
    You'll probably have to use some external tool, like 7zip as described [here](http://stackoverflow.com/questions/2195747/python-code-to-create-a-password-encrypted-zip-file). I couldn't find any free python library that is able to create encrypted zip files. – mata Apr 19 '15 at 17:44