<p>I am creating an ZIP file with <code>ZipFile</code> in Python 2.5, it works OK so far:</p>
<pre><code>import zipfile, os

locfile = &quot;test.txt&quot;
loczip = os.path.splitext (locfile)[0] + &quot;.zip&quot;
zip = zipfile.ZipFile (loczip, &quot;w&quot;)
zip.write (locfile)
zip.close()
</code></pre>
<p>But I couldn't find how to encrypt the files in the ZIP file.
I could use system and call <code>PKZIP -s</code>, but I suppose there must be a more &quot;Pythonic&quot; way.  I'm looking for an open source solution.</p>

[{'answer_id': 17287, 'author': 'Harley Holcombe', 'author_id': 1057, 'author_profile': 'https://Stackoverflow.com/users/1057', 'pm_score': -1, 'selected': False, 'text': '<p>You can use the <a href="http://www.chilkatsoft.com/python.asp" rel="nofollow noreferrer">Chilkat</a> library.  It\'s commercial, but has a free evaluation and seems pretty nice.</p>\n\n<p>Here\'s an example I got from <a href="http://www.example-code.com/python/zip.asp" rel="nofollow noreferrer">here</a>:</p>\n\n<pre><code>import chilkat\n\n# Demonstrates how to create a WinZip-compatible 128-bit AES strong encrypted zip\nzip = chilkat.CkZip()\nzip.UnlockComponent("anything for 30-day trial")\n\nzip.NewZip("strongEncrypted.zip")\n\n# Set the Encryption property = 4, which indicates WinZip compatible AES encryption.\nzip.put_Encryption(4)\n# The key length can be 128, 192, or 256.\nzip.put_EncryptKeyLength(128)\nzip.SetPassword("secret")\n\nzip.AppendFiles("exampleData/*",True)\nzip.WriteZip()\n</code></pre>\n'}, {'answer_id': 16050005, 'author': 'Shin Aoyama', 'author_id': 2288743, 'author_profile': 'https://Stackoverflow.com/users/2288743', 'pm_score': 5, 'selected': False, 'text': '<p>I created a simple library to create a password encrypted zip file in python. - <a href="https://github.com/smihica/pyminizip" rel="noreferrer"><strong>here</strong></a></p>\n\n<pre><code>import pyminizip\n\ncompression_level = 5 # 1-9\npyminizip.compress("src.txt", "dst.zip", "password", compression_level)\n</code></pre>\n\n<p><strong>The library requires zlib.</strong></p>\n\n<p>I have checked that the file can be extracted in WINDOWS/MAC.</p>\n'}, {'answer_id': 27443681, 'author': 'tripleee', 'author_id': 874188, 'author_profile': 'https://Stackoverflow.com/users/874188', 'pm_score': 3, 'selected': False, 'text': '<p>The duplicate question: <a href="https://stackoverflow.com/questions/2195747/code-to-create-a-password-encrypted-zip-file">Code to create a password encrypted zip file?</a> has an <a href="https://stackoverflow.com/a/2366917/355230">answer</a> that recommends using <code>7z</code> instead of <code>zip</code>.  My experience bears this out.</p>\n<p>Copy/pasting the answer by @jfs here too, for completeness:</p>\n<p>To create encrypted zip archive (named <code>\'myarchive.zip\'</code>) using open-source <a href="http://www.7-zip.org/" rel="nofollow noreferrer"><code>7-Zip</code></a> utility:</p>\n<pre><code>rc = subprocess.call([\'7z\', \'a\', \'-mem=AES256\', \'-pP4$$W0rd\', \'-y\', \'myarchive.zip\'] + \n                     [\'first_file.txt\', \'second.file\'])\n</code></pre>\n<p>To install 7-Zip, type:</p>\n<pre><code>$ sudo apt-get install p7zip-full\n</code></pre>\n<p>To unzip by hand (to demonstrate compatibility with zip utility), type:</p>\n<pre><code>$ unzip myarchive.zip\n</code></pre>\n<p>And enter <code>P4$$W0rd</code> at the prompt.</p>\n<p>Or the same in Python 2.6+:</p>\n<pre><code>&gt;&gt;&gt; zipfile.ZipFile(\'myarchive.zip\').extractall(pwd=\'P4$$W0rd\')\n</code></pre>\n'}, {'answer_id': 40164739, 'author': 'zqcolor', 'author_id': 5087657, 'author_profile': 'https://Stackoverflow.com/users/5087657', 'pm_score': 0, 'selected': False, 'text': "<p>@tripleee's answer helped me, see my test below.</p>\n\n<p>This code works for me on python 3.5.2  on Windows 8.1 ( <strong>7z</strong> path added to system).</p>\n\n<pre><code>rc = subprocess.call(['7z', 'a', output_filename + '.zip', '-mx9', '-pSecret^)'] + [src_folder + '/'])\n</code></pre>\n\n<p>With two parameters:</p>\n\n<ol>\n<li><code>-mx9</code> means max compression</li>\n<li><code>-pSecret^)</code> means password is <code>Secret^)</code>. <code>^</code> is escape for <code>)</code> for Windows OS, but when you unzip, it will need type in the <code>^</code>.</li>\n</ol>\n\n<p>Without <code>^</code> Windows OS will not apply the password when 7z.exe creating the <strong>zip</strong> file.</p>\n\n<p>Also, if you want to use <code>-mhe</code> switch, you'll need the file format to be in <strong>7z</strong> instead of <strong>zip</strong>.</p>\n\n<p>I hope that may help.</p>\n"}, {'answer_id': 57405348, 'author': 'Smack Alpha', 'author_id': 11090395, 'author_profile': 'https://Stackoverflow.com/users/11090395', 'pm_score': 2, 'selected': False, 'text': '<p><code>pyminizip</code> works great in creating a password protected zip file. For unziping ,it fails at some situations. Tested on python 3.7.3</p>\n\n<p>Here, i used pyminizip for encrypting the file.</p>\n\n<pre><code>import pyminizip\ncompression_level = 5 # 1-9\npyminizip.compress("src.txt",\'src\', "dst.zip", "password", compression_level)\n</code></pre>\n\n<p>For unzip, I used zip file module:</p>\n\n<pre><code>from zipfile import ZipFile\n\nwith ZipFile(\'/home/paulsteven/dst.zip\') as zf:\n    zf.extractall(pwd=b\'password\')\n</code></pre>\n'}, {'answer_id': 66158678, 'author': 'edif', 'author_id': 4779475, 'author_profile': 'https://Stackoverflow.com/users/4779475', 'pm_score': 3, 'selected': False, 'text': '<p>This thread is a little bit old, but for people looking for an answer to this question in 2020/2021.</p>\n<p>Look at <a href="https://pypi.org/project/pyzipper/" rel="noreferrer">pyzipper</a></p>\n<blockquote>\n<p>A 100% API compatible replacement for Python’s zipfile that can read and write AES encrypted zip files.</p>\n</blockquote>\n<p>7-zip is also a good choice, but if you do not want to use <code>subprocess</code>, go with pyzipper...</p>\n'}, {'answer_id': 70389456, 'author': 'Try2Code', 'author_id': 17393518, 'author_profile': 'https://Stackoverflow.com/users/17393518', 'pm_score': 1, 'selected': False, 'text': '<h4>You can use pyzipper for this task and it will work great when you want to encrypt a zip file or generate a protected zip file.</h4>\n<p><code>pip install pyzipper</code></p>\n<pre><code>import pyzipper\n\ndef encrypt_():\n\n    secret_password = b\'your password\'\n\n    with pyzipper.AESZipFile(\'new_test.zip\',\n                             \'w\',\n                             compression=pyzipper.ZIP_LZMA,\n                             encryption=pyzipper.WZ_AES) as zf:\n        zf.setpassword(secret_password)\n        zf.writestr(\'test.txt\', &quot;What ever you do, don\'t tell anyone!&quot;)\n\n    with pyzipper.AESZipFile(\'new_test.zip\') as zf:\n        zf.setpassword(secret_password)\n        my_secrets = zf.read(\'test.txt\')\n</code></pre>\n<blockquote>\n<p>The strength of the AES encryption can be configure to be 128, 192 or 256 bits. By default it is 256 bits. Use the setencryption() method to specify the encryption kwargs:</p>\n</blockquote>\n<pre><code>def encrypt_():\n    \n    secret_password = b\'your password\'\n\n    with pyzipper.AESZipFile(\'new_test.zip\',\n                             \'w\',\n                             compression=pyzipper.ZIP_LZMA) as zf:\n        zf.setpassword(secret_password)\n        zf.setencryption(pyzipper.WZ_AES, nbits=128)\n        zf.writestr(\'test.txt\', &quot;What ever you do, don\'t tell anyone!&quot;)\n\n    with pyzipper.AESZipFile(\'new_test.zip\') as zf:\n        zf.setpassword(secret_password)\n        my_secrets = zf.read(\'test.txt\')\n</code></pre>\n<p>Official Python ZipFile documentation is available here: <a href="https://docs.python.org/3/library/zipfile.html" rel="nofollow noreferrer">https://docs.python.org/3/library/zipfile.html</a></p>\n'}, {'answer_id': 72744952, 'author': 'Alex Deft', 'author_id': 10870968, 'author_profile': 'https://Stackoverflow.com/users/10870968', 'pm_score': 0, 'selected': False, 'text': '<p>2022 answer:</p>\n<p>I believe this is an utterly mundane task and therefore should be <em><strong>oneliner</strong></em>. I abstracted away all the frevolous details in a library that is as powerfull as a bash terminal.</p>\n<pre class="lang-py prettyprint-override"><code>from crocodile.toolbox import Path\n\nfile = Path(r\'my_string_path\')\nresult_file = file.zip(pwd=&quot;lol&quot;, use_7z=True)\n</code></pre>\n<ul>\n<li>when the 7z flag is raised, it gets called behind the scenes.\n<ul>\n<li>You don\'t need to learn 7z command line syntax.</li>\n<li>You don\'t need to worry about installing 7z, does that automatically if it\'s not installed. (tested on windows so far)</li>\n</ul>\n</li>\n</ul>\n'}]
['https://Stackoverflow.com/questions/17250', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/394/']