一、前言
本文實現RAR批量解壓的功能,通過python腳本調用WinRAR.exe解壓文件時似乎不會再有廣告框彈出 。
二、實現
通過python調用WinRAR.exe程序實現RAR文件的批量解壓,代碼如下:
import argparse
import os
【怎么實現RAR文件的批量解壓 rar解壓密碼是多少】class RarExtractor:
def __init__(self, in_dir=”./”, out_dir=”./”, pwds=None, exe=None):
self.in_dir = in_dir
self.out_dir = out_dir
self.pwds = pwds if pwds else [‘1234’]
self.exe = ‘”%s”‘ % exe if exe else ‘”C:Program FilesWinRARWinRAR.exe”‘
def extract_files(self, pwds, file_path, dst):
if not os.path.exists(dst):
os.mkdir(dst)
if os.path.isdir(dst) and os.path.isfile(file_path):
try:
for pwd in pwds:
extract_cmd = r’%s x -y -p%s %s %s’ % (self.exe, pwd, file_path, dst)
if os.system(extract_cmd) == 0:
print(“Extract %s OK.” % file_path)
return 0
else:
print(“Extract %s failed.” % file_path)
return -1
except RuntimeError:
print(“error”)
return -1
else:
print(‘File not exist’)
return -1
def extract_all_rar(self):
for root, dirs, files in os.walk(self.in_dir):
for f in files:
(filename, ext) = os.path.splitext(f)
if ext == ‘.rar’:
file_path = os.path.join(root, f)
print(file_path)
self.extract_files(self.pwds, file_path, os.path.join(self.out_dir, filename))
def _parse_options():
parser = argparse.ArgumentParser()
parser.add_argument(“–in_dir”, action=”store”, dest=”in_dir”, required=True, help=”Rar files dir”)
parser.add_argument(“–out_dir”, action=”store”, dest=”out_dir”, required=False, help=”Extracted file dir”)
parser.add_argument(“–pwds”, nargs=’+’, action=”store”, dest=”pwds”, required=False,
help=”Password list to extract Rar: –pwds 1111 2222 3333″)
parser.add_argument(“–exe”, action=”store”, dest=”exe”, required=False, help=”RAR exe install path”)
return parser.parse_args()
if __name__ == ‘__main__’:
options = _parse_options()
extractor = RarExtractor(options.in_dir, options.out_dir, options.pwds, options.exe)
extractor.extract_all_rar()
需要傳入的參數為:
–in_dir RAR文件所在目錄,默認是當前目錄
–out_dir 指定解壓后輸出的目錄,默認是當前目錄
–pwds 如果RAR是加密的,則需要指定解壓密碼,可以指定多個密碼,以空格隔開
–exe 指定WinRAR.exe所在的目錄,默認是”C:Program FilesWinRARWinRAR.exe”
三、測試
在目錄D:rar_test下新建3個txt文件,使用RAR加密壓縮,密碼為1024、2048和4096 。
通過以下命令測試:
python rar_extractor.py –in_dir D:rar_test –out_dir D:rar_test –pwds 1024 2048 4096 –exe “C:Program FilesWinRARWinRAR.exe”

文章插圖
- 無聊死了英語怎么說 去死的英文怎么說
- DNF氣功師首飾裝備 嗜靈之戒怎么換
- 單體瞬間輸出最厲害的職業 武魂玄冰冰霜系怎么加點
- 三年級怎么學英語 三年級如何學英語繪本,跟讀嗎
- 小學六年級英語該怎么學 六年級怎么學好英語
- 怎么提高孩子對英語的興趣 怎樣提高孩子學英語的興趣度
- 怎么培養孩子對英語的興趣 怎樣激發孩子對英語的興趣
- 鞋底有網格咯腳,可以增加一個厚一點的鞋墊來解決 鞋子底下有格子硌腳怎么辦
- 兔子耳朵辨別公母 兔子怎么分公母看耳朵
- 老壇酸蘿卜,姜,花椒,泡椒,山藥,枸杞。 潮汕老菜脯燉老鴨湯怎么做
