Unrar解压rar包
最近在下载很多附件,附件中有很多的格式,.doc,.xls,rar,zip等等。在处理这些附件的时候还是遇到了很多的坑,这里记录一下,希望对后来的朋友有所帮助。
Unrar解压缩rar包:
在linux环境中与windows环境后一些区别。
先说说windows环境的使用。
1.pip3 install unrar(安装unrar模块)
在from unrar import RarFile会报错LookupError: Couldn’t find path to unrar library
这个是环境的问题。意思是找不到 unrar library的路径,这里我们就需要去下载这个unrar library,事实上它就是UnRAR.dll这个东西,下载网址:http://www.rarlab.com/rar/UnRARDLL.exe 或者去http://www.rarlab.com/rar_add.htm找到UnRAR.dll下载,在lunix下应该需要自己编译。

2.设置环境变量(在python路径后加上unrar的安装路径,记得前面;别漏)
安装完后我电脑中的路径为C:\Program Files (x86)\UnrarDLL,win7 32位的朋友可以将它添加到环境变量中,64位的将其中的X64文件夹设置为环境变量,因为unrar模块识别的文件是unrar.dll和unrar.lib,所以将文件夹中的UnRAR.dll和UnRAR.lib用小写重命名。

Windows64位的还要将X64下面的UnRAR64.lib和UnRAR64.dall改为unrar.lib和unrar.dall去掉64且为小写。好了,重新尝试 from unrar import rarfile并运行,就成功了!(还报错的话,记得把编译器重启一下)

3.开始解压

from unrar import rarfile
file=rarfile.RarFile('filename.rar')
file.extractall('src')#src表示解压的路径

linux 使用Unrar加压.rar文件
linux中使用unrar解压rar文件相对来说更加的方便
直接sudo apt install unrar进行安装
之后就可以在shell中来使用相关的命令开始解压
当然如果文件比较多也可以结合os模块写成python的脚本来自动运行
这里说两个常用的命令:
unrar e filename.rar src :将filename.rar文件解压到路径为src的路径下面,e表示不处理rar包中文件路径(比如rar中有文件夹,文件夹下有图片,通过这种方式会直接把图片解压到解压的路径中)
rar x aa.rar 将aa.rar压缩文件解压到aa目录下,并保持原来压缩之前aa文件的目录组织结构

def unrar(self):unrar_list=os.listdir(self.path)for rar in unrar_list:if '.rar' in rar:file = os.path.splitext(rar)filename, type = filefilenames = filename.split('_')[0]#这里用os.system来实现shell命令os.system('unrar e /mnt/attachment/{} {}'.format(rar,self.unrar_path))lis=os.listdir(self.unrar_path)for file in lis:# pattern=re.findall('^(\d+).*?',file)if file=='Thumbs.db':os.remove(self.unrar_path+'/{}'.format('Thumbs.db'))

原文链接:https://blog.csdn.net/yyz_yinyuanzhang/article/details/85243551

from unrar import rarfile
import osimport itertools as its
import timefrom concurrent.futures import ThreadPoolExecutordef get_pwd(file_path, output_path, pwd):'''判断密码是否正确:param file_path: 需要破解的文件路径,这里仅对单个文件进行破解:param output_path: 解压输出文件路径:param pwd: 传入的密码:return:'''# 传入被解压的文件路径,生成待解压文件对象file = rarfile.RarFile(file_path)# 输出解压后的文件路径out_put_file_path = 'rar/{}'.format(file.namelist()[0])file.extractall(path=output_path, pwd=pwd)try:# 如果发现文件被解压处理,移除该文件os.remove(out_put_file_path)# 说明当前密码有效,并告知print('Find password is "{}"'.format(pwd))return Trueexcept Exception as e:# 密码不正确print('"{}" is nor correct password!'.format(pwd))# print(e)return Falsedef get_password(min_digits, max_digits, words):"""密码生成器:param min_digits: 密码最小长度:param max_digits: 密码最大长度:param words: 密码可能涉及的字符:return: 密码生成器"""while min_digits <= max_digits:pwds = its.product(words, repeat=min_digits)for pwd in pwds:yield ''.join(pwd)min_digits += 1file_path = 'rar/test.rar'
output_path = 'rar'# 密码范围
# words = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'  # 涉及到生成密码的参数
words = '01a'
pwds = get_password(4, 4, words)
# 开始查找密码
start = time.time()
while True:pwd = next(pwds)if get_pwd(file_path, output_path, pwd=pwd):break
end = time.time()
print('程序耗时{}'.format(end - start))

原文链接:https://www.jianshu.com/p/3d1d480e61f0

如上的代码,在实际测试过程中,会因为密码不对等问题,报错而无法进行,在查看了模块源代码后,进行一定修改。

这是unrar源代码(rarfile.py)中的一部分

class RarFile(object):"""RAR archive file."""def __init__(self, filename, mode='r', pwd=None):"""Load RAR archive file with mode read only "r"."""self.filename = filenamemode = constants.RAR_OM_LIST_INCSPLITarchive = unrarlib.RAROpenArchiveDataEx(filename, mode=mode)handle = self._open(archive)# assert(archive.OpenResult == constants.SUCCESS)self.pwd = pwdself.filelist = []self.NameToInfo = {}if archive.CmtState == constants.RAR_COMMENTS_SUCCESS:self.comment = archive.CmtBuf.valueelse:self.comment = Noneself._load_metadata(handle)self._close(handle)

修改代码如下:

from unrar import rarfile
import osimport itertools as its
import timefrom concurrent.futures import ThreadPoolExecutordef get_pwd(file_path, output_path, pwd):'''判断密码是否正确:param file_path: 需要破解的文件路径,这里仅对单个文件进行破解:param output_path: 解压输出文件路径:param pwd: 传入的密码:return:'''try:# 传入被解压的文件路径,生成待解压文件对象file = rarfile.RarFile(file_path, pwd=pwd)# 输出解压后的文件路径# out_put_file_path = output_path# print(file_path,output_path)file.extractall(output_path)# 如果发现文件被解压处理,移除该文件# os.remove(out_put_file_path)# 说明当前密码有效,并告知print('Find password is "{}"'.format(pwd))return Trueexcept Exception as e:# 密码不正确print('"{}" is not correct password!'.format(pwd))# print(e)return Falsedef get_password(min_digits, max_digits, words):"""密码生成器:param min_digits: 密码最小长度:param max_digits: 密码最大长度:param words: 密码可能涉及的字符:return: 密码生成器"""while min_digits <= max_digits:pwds = its.product(words, repeat=min_digits)for pwd in pwds:yield ''.join(pwd)min_digits += 1# 压缩文件绝对地址
file_path = 'F:\W3SchoolAPI20170311\1900579008.rar'
# 输出文件绝对地址
output_path = 'F:\W3SchoolAPI20170311'# 密码范围
words = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'  # 涉及到生成密码的参数
# words = '12345'
pwds = get_password(4, 8, words)# 开始查找密码
start = time.time()
while True:pwd = next(pwds)# pwd = "2222"if get_pwd(file_path, output_path, pwd=pwd):breakend = time.time()
print('程序耗时{}'.format(end - start))

这样太慢了,再压榨下计算机性能吧,再优化代码如下。

from unrar import rarfile
import osimport itertools as its
import timefrom multiprocessing import Pool
import queue
import threadingdef get_pwd(file_path, output_path, pwd):'''判断密码是否正确:param file_path: 需要破解的文件路径,这里仅对单个文件进行破解:param output_path: 解压输出文件路径:param pwd: 传入的密码:return:'''try:# 传入被解压的文件路径,生成待解压文件对象file = rarfile.RarFile(file_path, pwd=pwd)# 输出解压后的文件路径out_put_file_path = output_path# print(file_path,output_path)file.extractall(output_path)# 如果发现文件被解压处理,移除该文件# os.remove(out_put_file_path)# 说明当前密码有效,并告知print('Find password is "{}"'.format(pwd))return True,pwdexcept Exception as e:# 密码不正确print('"{}" is not correct password!'.format(pwd))# print(e)return False,pwddef get_password(min_digits, max_digits, words):"""密码生成器:param min_digits: 密码最小长度:param max_digits: 密码最大长度:param words: 密码可能涉及的字符:return: 密码生成器"""while min_digits <= max_digits:pwds = its.product(words, repeat=min_digits)for pwd in pwds:yield ''.join(pwd)min_digits += 1if __name__=="__main__":file_path = 'XX.rar'output_path = 'F:\XX'# 密码范围words = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'  # 涉及到生成密码的参数# words = '12345'pwds = get_password(4, 8, words)# 开始查找密码start = time.time()# judge = []result=queue.Queue(maxsize=10) #队列pool = Pool()def pool_th():while True: ##这里需要创建执行的子进程非常多pwd = next(pwds)try:result.put(pool.apply_async(get_pwd, args=(file_path, output_path, pwd)))except:breakdef result_th():while True:#pwd = next(pwds)a=result.get().get() #获取子进程返回值print(a)if a[0]:#print(pwd)pool.terminate() #结束所有子进程break'''利用多线程,同时运行Pool函数创建执行子进程,以及运行获取子进程返回值函数。'''t1=threading.Thread(target=pool_th)t2=threading.Thread(target=result_th)t1.start()t2.start()t1.join()t2.join()pool.join()end = time.time()print('程序耗时{}'.format(end - start))

破解rar压缩文件密码(Windows环境)相关推荐

  1. macos\Linux下使用fcrackzip破解zip压缩文件密码

    加密解密:http://www.lybbn.cn/data/datas.php?yw=133 1.fcrackzip简介 fcrackzip是一款专门破解zip类型压缩文件密码的工具,工具小巧方便.破 ...

  2. zip、rar压缩文件密码破解——使用ARCHPR Professional Edition

    直链下载地址: https://pan.abn.cc/weiyun/down.php?u=82441366e3c1f43fc69210e8ece93470.undefined.zip (压缩包内含解压 ...

  3. 如何破解压缩文件密码-省时省力的方法

    压缩文件破解工具下载地址:http://www.cnblogs.com/spring_wang/archive/2013/06/14/3135163.html 应该很多人都碰到过RAR加密.解密的问题 ...

  4. rar压缩文件暴力破解

    1. 简介 rar 压缩文件资源又不少是被加密的,密码通常也比较简单,我们可以通过暴力破解的方式来获取,通常耗时也比较小. 2. 使用说明 2.1 基本语法 rar-bruteforce-crack. ...

  5. 破解压缩文件密码rarcrack

    2019独角兽企业重金招聘Python工程师标准>>> 破解压缩文件密码rarcrack 常见的压缩文件格式有ZIP.RAR和7z.这三种格式都支持使用密码进行加密压缩.前面讲过破解 ...

  6. 压缩文件如何设置密码?/ 如何破解压缩文件密码?

    一.压缩文件设置密码 步骤:文件-右键-添加到压缩文件-添加密码 或者其他压缩软件 二.破解压缩文件密码 破解工具:Advanced Archive Password Recovery 4位以内密码免 ...

  7. ubuntu18.04怎么解压rar压缩文件

    ubuntu18.04怎么解压rar压缩文件 今天在自己的ubuntu18.04中得到一个rar的压缩文件,在windows下使用的winrar软件,在这里又安装不上,后来发现有一些开源的解压软件可以 ...

  8. ZIP RAR 压缩文件解密工具,亲测有效

    ZIP-RAR文件解密工具,亲测有效 相信不少用户从网上下载的资源的时候遇到过加密的压缩ZIP.RAR文件,明明唾手可得的资源,但是却有密码,真是让人不开心,于是,我们便想方设法的破解这个压缩文件,但 ...

  9. RAR压缩文件如何转换成ZIP格式?

    压缩文件有多种不同的格式,有时候因为需求不同,我们需要把RAR压缩文件转换成ZIP格式,那要如何操作呢?下面小编分享2种简单的方法. 方法一: 如果需要转换的RAR压缩包不是很多,我们可以直接把文件名 ...

最新文章

  1. 首部高中《人工智能基础》教材问世,40家中学引入
  2. 为什么抢红包抢不过别人?学了这个算法就明白了!
  3. linq调用mysql函数_如何为linq对象制作一个展平函数(Linq To Entities for mysql)?
  4. linux打开二进制文件后终端乱码处理
  5. 吴恩达机器学习练习4:神经网络学习(损失函数和正则化)
  6. SSM框架项目的pom导入包和xml配置
  7. CODE[VS] 1098 均分纸牌 ( 2002年NOIP全国联赛提高组)
  8. 前端学习-jQuery源码学习
  9. 创业阶段如何找客户_如何找创业合伙人
  10. 重构Webpack系列之二 ---- 入口起点
  11. IE11降级到IE8
  12. 基于SSM 的图书馆管理系统
  13. dedecms pm.php,dedecms /member/pm.php SQL Injection Vul
  14. 阿里巴巴使用的Rax源码
  15. 我的ROS学习之路——动起来的小海龟
  16. PHP前一页 后一页 切换
  17. 汇金增持:救市还是抄底? FT中文网特约撰稿人 陈宁远
  18. 华为onu 调为交换机_华为MA5620 ONU/MDU如何配置成交换机?
  19. Google实用高级搜索技巧总结
  20. 案例 :探索性文本数据分析的新手教程(Amazon案例研究)

热门文章

  1. GEEer成长日记二十:使用Sentinel 2影像计算水体指数NDWI、MNDWI并下载到本地
  2. unity设置玩家上下左右移动、摄像机跟随、UI界面
  3. 二级网页打不开解决方案 (轉摘)
  4. 商业地图 | 成都人有多爱喝茶--茶馆地图
  5. 【Python爬虫】爬了七天七夜,终于爬出了博客园粉丝数排行榜!
  6. TCP重传机制有哪些?
  7. 2017.10.16 队内互测 D4
  8. itext报错PdfReader not opened with owner password
  9. web/b.s扑克斗地主小小完工
  10. memcached 缓存命中率