9.1 压缩包密码解压

from zipfile import ZipFile
import os

def passwd(path):
    file_type = os.path.splitext(path)
    print(file_type) # ('./办公自动化/files/合并PDF-密码', '.zip')
    type = file_type[-1][1:]
    print(type)   # zip

    if os.path.splitext(path)[-1][1:] == 'zip':
        with ZipFile(path, 'r') as zip:
            password = '123456'
            zip.extractall('./办公自动化/files/合并PDF-密码', pwd=password.encode("utf8"))

def zip_pwd(path):

    import pyzipper
    import shutil
    import os

    password = '123456'

    with pyzipper.AESZipFile(path, 'r', compression=pyzipper.ZIP_DEFLATED, encryption=pyzipper.WZ_AES) as zf:
        for name in zf.namelist():
            print(name)  # ║╧▓óPDF.pdf
            # 假设 ZIP 内部文件名编码为 GBK,转换为 gbk
            gbk_name = name.encode('cp437').decode('gbk')
            print(gbk_name)

            zf.extract(name, './办公自动化/files/合并PDF-非经典密码', pwd=str.encode(password))
            os.chdir('./办公自动化/files/合并PDF-非经典密码')
            shutil.move(name, gbk_name)

            # zf.extractall('./办公自动化/files/合并PDF-非经典密码', pwd=str.encode(password))

if __name__ == '__main__':
    # passwd('./办公自动化/files/合并PDF-密码.zip')    # zip 压缩经典密码方式
    # zip_pwd('./办公自动化/files/合并PDF-非经典密码.zip')  # zip WZ_AES 新加密方式
    zip_pwd('./办公自动化/files/合并PDF-密码.zip')