Create an encrypted ZIP file in Python

后端 未结 5 765
一向
一向 2020-11-28 07:02

I am creating an ZIP file with ZipFile in Python 2.5, it works ok so far:

import zipfile, os

locfile = \"test.txt\"
loczip = os.path.splitext (locfile)[0] +         


        
相关标签:
5条回答
  • 2020-11-28 07:33

    You can use the Chilkat library. It's commercial, but has a free evaluation and seems pretty nice.

    Here's an example I got from here:

    import chilkat
    
    # Demonstrates how to create a WinZip-compatible 128-bit AES strong encrypted zip
    zip = chilkat.CkZip()
    zip.UnlockComponent("anything for 30-day trial")
    
    zip.NewZip("strongEncrypted.zip")
    
    # Set the Encryption property = 4, which indicates WinZip compatible AES encryption.
    zip.put_Encryption(4)
    # The key length can be 128, 192, or 256.
    zip.put_EncryptKeyLength(128)
    zip.SetPassword("secret")
    
    zip.AppendFiles("exampleData/*",True)
    zip.WriteZip()
    
    0 讨论(0)
  • 2020-11-28 07:34

    I created a simple library to create a password encrypted zip file in python. - here

    import pyminizip
    
    compression_level = 5 # 1-9
    pyminizip.compress("src.txt", "dst.zip", "password", compression_level)
    

    The library requires zlib.

    I have checked that the file can be extracted in WINDOWS/MAC.

    0 讨论(0)
  • 2020-11-28 07:35

    pyminizip works great in creating a password protected zip file. For unziping ,it fails at some situations. Tested on python 3.7.3

    Here, i used pyminizip for encrypting the file.

    import pyminizip
    compression_level = 5 # 1-9
    pyminizip.compress("src.txt",'src', "dst.zip", "password", compression_level)
    

    For unzip, I used zip file module:

    from zipfile import ZipFile
    
    with ZipFile('/home/paulsteven/dst.zip') as zf:
        zf.extractall(pwd=b'password')
    
    0 讨论(0)
  • 2020-11-28 07:37

    The duplicate question https://stackoverflow.com/a/2366917/874188 recommends using 7z instead of zip. My experience bears this out.

    Copy/pasting the answer by @JFSebastian here too for completeness:

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

    rc = subprocess.call(['7z', 'a', '-mem=AES256', '-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 utility), 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')
    
    0 讨论(0)
  • 2020-11-28 07:44

    @tripleee's answer helped me, see my test below.

    This code works for me on python 3.5.2 on Windows 8.1 ( 7z path added to system).

    rc = subprocess.call(['7z', 'a', output_filename + '.zip', '-mx9', '-pSecret^)'] + [src_folder + '/'])
    

    With two parameters:

    1. -mx9 means max compression
    2. -pSecret^) means password is Secret^). ^ is escape for ) for Windows OS, but when you unzip, it will need type in the ^.

    Without ^ Windows OS will not apply the password when 7z.exe creating the zip file.

    Also, if you want to use -mhe switch, you'll need the file format to be in 7z instead of zip.

    I hope that may help.

    0 讨论(0)
提交回复
热议问题