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 = file
filenames = 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 os
import itertools as its
import time
from concurrent.futures import ThreadPoolExecutor
def 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 True
except Exception as e:
# 密码不正确
print('"{}" is nor correct password!'.format(pwd))
# print(e)
return False
def 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 = '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 = filename
mode = constants.RAR_OM_LIST_INCSPLIT
archive = unrarlib.RAROpenArchiveDataEx(filename, mode=mode)
handle = self._open(archive)
# assert(archive.OpenResult == constants.SUCCESS)
self.pwd = pwd
self.filelist = []
self.NameToInfo = {}
if archive.CmtState == constants.RAR_COMMENTS_SUCCESS:
self.comment = archive.CmtBuf.value
else:
self.comment = None
self._load_metadata(handle)
self._close(handle)
修改代码如下:
from unrar import rarfile
import os
import itertools as its
import time
from concurrent.futures import ThreadPoolExecutor
def 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
except Exception as e:
# 密码不正确
print('"{}" is not correct password!'.format(pwd))
# print(e)
return False
def 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):
break
end = time.time()
print('程序耗时{}'.format(end - start))
这样太慢了,再压榨下计算机性能吧,再优化代码如下。
from unrar import rarfile
import os
import itertools as its
import time
from multiprocessing import Pool
import queue
import threading
def 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,pwd
except Exception as e:
# 密码不正确
print('"{}" is not correct password!'.format(pwd))
# print(e)
return False,pwd
def 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
if __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:
break
def 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))