如何使用密码在python中压缩文件夹? [英] How to zip a folder in python with password?

查看:41
本文介绍了如何使用密码在python中压缩文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 pyminizip 我可以在 python 中使用密码压缩文件:

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)

但是我如何使用密码将整个文件夹myFolder"压缩到一个 zip 文件中?我尝试从路径中删除文件名,但它给出了错误

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

下面的链接有一个压缩目录的功能.但它不会添加密码.

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/

如果有人可以告诉我是否可以将密码添加到现有的 zip 文件中,那将解决我的问题.这可能吗?

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?

推荐答案

我终于能够使用 Anupam Chaplot 建议的名为pyzipper"的库完成对整个目录(包括所有子文件夹结构和文件)的加密.

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.

这是解决方案:

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()

由于我是 Python 新手,我无法详细解释代码.以下是参考资料:

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/

在 windows 中提取生成的 ZIP 文件:

To extract the Generated ZIP file in windows :

右键单击 -> 解压缩(已加密)

如果直接点击Extract All选项,会报错

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

这篇关于如何使用密码在python中压缩文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆