17

What is the Python code to create a password encrypted zip file? I'm fine with using some apt-get'able utilities using system on the command line.

martineau
  • 119,623
  • 25
  • 170
  • 301
MikeN
  • 45,039
  • 49
  • 151
  • 227

5 Answers5

35

To create encrypted zip archive (named 'myarchive.zip') using open-source 7-Zip utility:

rc = subprocess.call(['7z', 'a', '-pP4$$W0rd', '-y', 'myarchive.zip'] + 
                     ['first_file.txt', 'second.file'])

To install 7-Zip, type:

$ sudo apt-get install p7zip-full

To unzip by hand (to demonstrate compatibility with zip utitity), type:

$ unzip myarchive.zip

And enter P4$$W0rd at the prompt.

Or the same in Python 2.6+:

>>> zipfile.ZipFile('myarchive.zip').extractall(pwd='P4$$W0rd')
jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • 2
    +1 7-Zip is available on Windows also. It supports many compression/archive formats, not just zip. – John Machin Mar 02 '10 at 22:22
  • zipfile.ZipFile does not create an encrypted zip file. It can only read from encrypted zip files. pyminizip does this as stated in the link. – shadowbq Dec 08 '14 at 20:15
  • @shadowbq: yes. `ZipFile` can *extract* (*unzip*) the encrypted archive. If it could create the encrypted archives; you wouldn't need `7z` utility. `pyminizip` hasn't existed in 2010. I don't know how convenient (to install) or reliable (compared to `7z`) `pyminizip` is -- its development status is *alpha*. – jfs Dec 12 '14 at 16:25
  • Hi I tried this solution, on linux (Ubuntu) when I open the file, the app ask me a password but on mac os the app dosn't ask me anything and open the file (without insert any password). Why?? I use this script {code} rc = subprocess.Popen('7z a -p'+PASSWORD+' -y '+ newFileName + ' ' +file , shell=True) {/code} – Paolo Feb 27 '15 at 09:06
  • @Paolo: It is not the same code. Why would you use `shell=True` here? Or are you suggesting that an encrypted 7z archive created on Ubuntu can be read on OS X without a password? – jfs Feb 27 '15 at 09:32
  • Hi Sebastian, the second one. On Ubuntu ask me the password in Yosemite no. The difference is only the python function that I call but the bash command is the same. – Paolo Feb 27 '15 at 11:04
  • Hi Sebastian, thanks for your fast replay, it was my mistake with files. All is fine. – Paolo Feb 27 '15 at 11:13
  • 2
    On Windows, I had to replace '7z' by 'C:\\Program Files\\7-Zip\\7z.exe' – fstevens Jun 06 '17 at 15:04
  • 1
    Can use [`subprocess.run`](https://docs.python.org/3/library/subprocess.html#subprocess.run) instead of `subprocess.call` on Python 3.5+. – ytu Nov 08 '18 at 07:35
6

Extraction is pretty easy, you just use zipfile.ZipFile.setpassword() which was introduced in python 2.6, however the standard python library lacks support for creating encrypted zip files.

There are commercially available libraries for Python which supports creation of encrypted and password protected zip files. If you want to use something freely available, you need to use the standard zip command line utility.

zip -e -Ppassword filename.zip fileA fileB ...

Johan Dahlin
  • 25,300
  • 6
  • 40
  • 55
  • I don't see `-P` on the standard `zip` utility. `zip --help | grep -i -e '-p'` returns nothing (Ubuntu, Zip 3.0 (July 5th 2008), by Info-ZIP). I use open-source solution in my answer: http://stackoverflow.com/questions/2195747/python-code-to-create-a-password-encrypted-zip-file/2366917#2366917 – jfs Mar 02 '10 at 21:44
5

Actually setpassword("yourpassword") is only valid for extracting, not for creating a zip file.

The solution (not to my liking):

How to create an encrypted ZIP file?

martineau
  • 119,623
  • 25
  • 170
  • 301
Diego Castro
  • 3,458
  • 4
  • 35
  • 42
1

If Python is not a must and you can use system utilities, tools like zip or rar provides password encrypted compression. zip with -e option, and rar with -p.

ghostdog74
  • 327,991
  • 56
  • 259
  • 343
0

You can use Pygpgme to create a password-protected gpg file, which is compressed.

You'll need to use the equivalent of gpg -c myFile or gpg --symmetric myFile and gpg myFile.gpg

I don't know what the equivalents are in that Python module, but I know they've existed since version 0.2. There was a bug report before then mentioning the lack of it, but someone released a patch and they fixed it in version 0.2.

This uses symmetric encryption so you don't have to worry about keys.

You might find my post asking how to use it on UbuntuForums. Feel free to answer it if you know.

Mark
  • 1