How to zip a folder in python with password?

[亡魂溺海] 提交于 2020-03-04 21:34:32

问题


With pyminizip i am able to zip a file with password in python :

filepath=r"C:\Users\xxx\Desktop\myFolder\file.txt"

import pyminizip
pyminizip.compress(filepath, None,"output.zip", "password", 0)

But how do I zip the whole folder 'myFolder' into a zip file with password? I tried removing the filename from the path but it gives the error

OSError: error in opening C:\Users\xxx\Desktop\myFolder for reading

EDIT :

The below link has a function which will zip the directory. But It wont add a password.

https://www.calazan.com/how-to-zip-an-entire-directory-with-python/

If anyone can let me know if it is possible to add a password to an existing zip file, that will solve my problem. Is that possible?


回答1:


Try this: Firstly check here please for pynzip. After that try it.

import pyminizip as pyzip

compression = 8
pyzip.compress("test.txt", "test.zip", "Pswrd", compression)



回答2:


I was finally able to accomplish encryping the whole directory(including all subfolder struncture and files) using a library called 'pyzipper' suggested by Anupam Chaplot.

Here is the solution :

def zip_folderPyzipper(folder_path, output_path):
    """Zip the contents of an entire folder (with that folder included
    in the archive). Empty subfolders will be included in the archive
    as well.
    """
    parent_folder = os.path.dirname(folder_path)
    # Retrieve the paths of the folder contents.
    contents = os.walk(folder_path)
    try:
        zip_file = pyzipper.AESZipFile('new_test.zip','w',compression=pyzipper.ZIP_DEFLATED,encryption=pyzipper.WZ_AES)
        zip_file.pwd=b'PASSWORD'
        for root, folders, files in contents:
            # Include all subfolders, including empty ones.
            for folder_name in folders:
                absolute_path = os.path.join(root, folder_name)
                relative_path = absolute_path.replace(parent_folder + '\\',
                                                      '')
                print ("Adding '%s' to archive." % absolute_path)
                zip_file.write(absolute_path, relative_path)
            for file_name in files:
                absolute_path = os.path.join(root, file_name)
                relative_path = absolute_path.replace(parent_folder + '\\',
                                                      '')
                print ("Adding '%s' to archive." % absolute_path)
                zip_file.write(absolute_path, relative_path)

        print ("'%s' created successfully." % output_path)

    except IOError as message:
        print (message)
        sys.exit(1)
    except OSError as message:
        print(message)
        sys.exit(1)
    except zipfile.BadZipfile as message:
        print (message)
        sys.exit(1)
    finally:
        zip_file.close()

Since I am new in python i cant explain the code in detail. Here are the references :

https://pypi.org/project/pyzipper/

https://www.calazan.com/how-to-zip-an-entire-directory-with-python/

To extract the Generated ZIP file in windows :

Right Click - > Unzip(Encripted)

If you directly click Extract All option, then it will give error



来源:https://stackoverflow.com/questions/60087965/how-to-zip-a-folder-in-python-with-password

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!