python rar解压

本文介绍了如何使用Python的unrar库解压RAR文件,包括安装UnRARDLL、设置环境变量以及提供了一个暴力破解密码的示例。同时,还提到了解码zip文件以处理中文编码问题的方法。
摘要由CSDN通过智能技术生成

1

pip install unrar

2
https://www.rarlab.com/rar/UnRARDLL.exe这个地址没了。
用这个

https://www.rarlab.com/rar/unrardll-701b1.exe

默认安装
设置环境变量
变量名:UNRAR_LIB_PATH
变量值:C:\Program Files (x86)\UnrarDLL\x64\UnRAR64.dll
将路径C:\Program Files (x86)\UnrarDLL\Examples\MASM下的runrar.exe放到python路径的Scripts文件夹下,用anaconda3的放到anaconda3\Scripts下,重启pycharm,让重新构建索引,重启步骤:文件-清除缓存-清除并重启

解压rar文件代码如下:

from unrar import rarfile
path = r'.\压缩包密码破解\压缩包.rar'
# fileData = zipfile.ZipFile(path)
fileData = rarfile.RarFile(path)
fileData.extractall(path=r".\压缩包密码破解\\", pwd='778899')

# zip不乱码
# def support_gbk(zip_file: ZipFile):
#     name_to_info = zip_file.NameToInfo
#     # copy map first
#     for name, info in name_to_info.copy().items():
#         real_name = name.encode('cp437').decode('gbk')
#         if real_name != name:
#             info.filename = real_name
#             del name_to_info[name]
#             name_to_info[real_name] = info
#     return zip_file
#
#
# with support_gbk(fileData) as zfp:
#     zfp.extractall(r".\压缩包密码破解", pwd=''.encode())

暴力破解(感觉没用,时间要很久)
代码如下(示例):

import zipfile
import random
import time
import sys
from zipfile import ZipFile

from unrar import rarfile


class MyIterator():
    # 单位字符集合
    letters = '0123456789' # 生成使用字符
    min_digits = 0
    max_digits = 0

    def __init__(self, min_digits, max_digits):
        # 实例化对象时给出密码位数范围,一般4到10位
        if min_digits < max_digits:
            self.min_digits = min_digits
            self.max_digits = max_digits
        else:
            self.min_digits = max_digits
            self.max_digits = min_digits

    # 迭代器访问定义
    def __iter__(self):
        return self

    def __next__(self):
        # rst1 = str()
        # rst2 = str()
        # rst3 = str()
        # for item in range(0, random.randrange(self.min_digits, self.max_digits + 1)):
        #     rst1 += random.choice(MyIterator.letters)
        #     rst2 += random.choice(MyIterator.letters)
        #     rst3 += random.choice(MyIterator.letters)
        # return ''.join([rst1, rst2, rst3])
        rst = str()
        for item in range(0, random.randrange(self.min_digits, self.max_digits + 1)):
            rst += random.choice(MyIterator.letters)
        return rst


def support_gbk(zip_file: ZipFile):
    name_to_info = zip_file.NameToInfo
    # copy map first
    for name, info in name_to_info.copy().items():
        real_name = name.encode('cp437').decode('gbk')
        if real_name != name:
            info.filename = real_name
            del name_to_info[name]
            name_to_info[real_name] = info
    return zip_file


def extract(path):
    start_time = time.time()
    fileData = ''
    types = ''
    if '.zip' in path:
        fileData = zipfile.ZipFile(path)
        fileData = support_gbk(fileData)
        types = 'zip'
    elif '.rar' in path:
        fileData = rarfile.RarFile(path)
        types = 'rar'
    if fileData == '':
        print('不是zip或rar文件')
    openFile_s(fileData, start_time, types)
    fileData.close()


def openFile_s(fileData, start_time,types):
    # for i in range(7, 8):
    #     maxCount = i * 100000

    i = 6 # 生成密码长度
    counts = 1
    for p in MyIterator(i, i):
        try:
            print('{}正在测试密码:{}'.format(counts, p))
            if types == 'zip':
                fileData.extractall(path=r".\压缩包密码破解", pwd=str(p).encode('utf-8'))
            if types == 'rar':
                fileData.extractall(path=r".\压缩包密码破解", pwd=str(p))
            print("the password is {}".format(p))
            now_time = time.time()
            s = now_time - start_time
            print("spend time is {}时{}分{}秒".format(int(s // 60 // 60), int(s // 60 % 60), round(s % 60,2)))
            sys.exit(0)
        except Exception as e:
            print(e)
            counts += 1
            # if counts == maxCount:
            #     break
            pass


if __name__ == '__main__':
    path = r'.\压缩包密码破解\压缩包1.rar'
    # path = r'.\压缩包密码破解\压缩包2.zip'
    extract(path)

用到 https://blog.csdn.net/zhouchen1998/article/details/82015318 这个网页代码

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值