Python__模块(office-压缩文件)__zipfile / rarfile

安装

  • pip install rarfile
  • pip install zipfile
  • pip install pyzipper

了解

rar 和 .zip 是两种常见的压缩文件格式,相同的原始数据

使用.rar 要比.zip 节省不少磁盘空间。

winRaR 目前似乎并没有现成的第三pyhton库,目前只能使用Python 调用 Windows 系统下的 CMD 命令进行WinRAR压缩。


参考代码

RAR文件破解(低配版)

import rarfile
''' 添加环境变量(C:\Program Files\WinRAR) '''
def rar_put():
    rf = rarfile.RarFile("./pw.rar")  # 待解压文件
    rf.extractall("./")

def rar_pw():
    pwlist = ["111", "222", "123", "333"]  # 密码本
    index = 0
    while True:
        try:
            rf = rarfile.RarFile("./pwtest.rar")  # 待解压文件
            rf.extractall(pwd=pwlist[index].encode("utf-8"))
            print("------------------")
            print("密码正确:", pwlist[index])
            break
        except Exception:
            print("密码尝试:", pwlist[index])
            index = index + 1

ZIP文件破解(低配版)

"""
当您使用例如 7zip 创建 zip 文件时,此 zip 文件将被加密。
但是加密不是以字节为单位的,而是在哈希中加密的:AES-256 或 ZipCrypto。
"""
import pyzipper
pwlist = ["123", "1234", "12345", "123456", "1234567"]
index = 0
while True:
    try:
        with pyzipper.AESZipFile(
            "./zip.zip",
            "r",
            compression=pyzipper.ZIP_DEFLATED,
            encryption=pyzipper.WZ_AES,
        ) as extracted_zip:
            extracted_zip.extractall(pwd=str.encode(pwlist[index]))
            print("------------------------")
            print("密码通过: ", pwlist[index])
            break
    except RuntimeError:
        print("密码测试: ", pwlist[index])
        index += 1

ZIP文件压缩

import zipfile

def zip_files(files, zip_name):
    # 参数:压缩文件名,模式,压缩方法
    with zipfile.ZipFile(zip_name, "w", zipfile.ZIP_DEFLATED) as zip:
        for file in files:
            print("compressing", file)
            # 添加到压缩文件
            zip.write(file)
        # 将zip文件对象关闭
        zip.close()
        print("compressing finished")

# 文件的位置,多个文件用“,”隔开
files = ["./123.txt", "./456.txt"]
zip_file = "./new.zip"  # 压缩包名字
zip_files(files, zip_file)

# 获取
"""
zip=zipfile.ZipFile('./zip.zip')
file_name=zip.namelist() # 获取所有文件名称
"""

WinRAR.exe压缩文件

import os
# 注释
"""
    inputPath   待压缩的文件路径
    inputFile   待压缩的文件名
    outputFile  压缩后的文件
"""
def compress_rar(inputPath, inputFile, outputFile, rarPath="C:/Program Files/WinRAR/WinRAR.exe"):
    # 调用WinRaR.exe程序
    cmd_command = r"%s a %s %s" % (rarPath, outputFile, inputFile)
    os.chdir(inputPath)              # WinRaR切换工作目录
    result = os.system(cmd_command)  # 执行压缩命令
    if result == 0:
        print("Successful Compress", inputFile)
    else:
        print("FAILED Compress", inputFile)
    # 示例:compress_rar('./1.txt','new.rar','./')

compress_rar(
    "C:/Users/Administrator/Desktop/",
    "123.txt",
    "./test.rar",
)

  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

vip飞梦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值