Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code to create a password encrypted zip file? [duplicate]

Tags:

python

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.

like image 716
MikeN Avatar asked Feb 03 '10 21:02

MikeN


3 Answers

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')
like image 87
jfs Avatar answered Oct 15 '22 06:10

jfs


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 ...

like image 37
Johan Dahlin Avatar answered Oct 15 '22 04:10

Johan Dahlin


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?

like image 44
Diego Castro Avatar answered Oct 15 '22 05:10

Diego Castro