濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > 用python批量解壓帶密碼的壓縮包

用python批量解壓帶密碼的壓縮包

熱門標(biāo)簽:北瀚ai電銷機(jī)器人官網(wǎng)手機(jī)版 小蘇云呼電話機(jī)器人 北京電銷外呼系統(tǒng)加盟 市場(chǎng)上的電銷機(jī)器人 佛山400電話辦理 地圖標(biāo)注面積 所得系統(tǒng)電梯怎樣主板設(shè)置外呼 朝陽手機(jī)外呼系統(tǒng) 儋州電話機(jī)器人

項(xiàng)目地址:

https://github.com/Mario-Hero/toolUnRar

環(huán)境需求

  • Windows系統(tǒng)
  • Python 3
  • 對(duì)于解壓RAR文件,需要安裝WinRAR
  • 對(duì)于解壓7z/zip等其他7-Zip支持解壓的文件,需要安裝7-Zip

用法 Usage

直接拖入文件夾或壓縮文件即可批量解壓縮包含密碼的壓縮文件。如果拖入的是文件夾,則會(huì)把該文件夾下的壓縮文件解壓縮,但不進(jìn)入下一級(jí)目錄。通過設(shè)置PASSWD來設(shè)置字典,通過設(shè)置DELETEIT來設(shè)置解壓后是否刪除被成功解壓的壓縮文件。本腳本會(huì)通過文件的后綴識(shí)別該文件是否為壓縮文件。

你可以把WinRAR目錄下的Unrar.exe和7-Zip目錄下的7z.exe直接復(fù)制到這個(gè)toolUnRar.py文件的相同目錄下,這樣就可以攜帶使用了。

參數(shù) Parameters

  • PASSWD = ["hello","123456"] :你的密碼本,該腳本會(huì)從這個(gè)數(shù)組中不斷試驗(yàn)密碼來解壓縮,直到成功為止。
  • DELETEIT :一個(gè)危險(xiǎn)的參數(shù)。為真時(shí),該腳本會(huì)直接刪除成功解壓的壓縮文件。為假則不會(huì)刪除。
  • LOC_WINRAR = "C:\Program Files\WinRAR\" 你的WinRAR安裝位置。就算這個(gè)變量的設(shè)置的不對(duì),該程序也會(huì)在可能的位置來尋找對(duì)應(yīng)的程序。
  • LOC_7Z:7-Zip的安裝位置。
  • SAVE_MODE = True:如果該腳本無法通過后綴判斷這是不是壓縮文件,則不對(duì)該文件進(jìn)行操作。

完整代碼

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

# Created by Mario Chen, 04.04.2021, Shenzhen
# My Github site: https://github.com/Mario-Hero

import sys
import os
import subprocess

# you can change it >>>>>

PASSWD     = ["123456","hello"]  # the possible passwords
DELETEIT   = False                                     # DANGER!! If it is True,will delete rar file after extraction
LOC_WINRAR = "C:\\Program Files\\WinRAR\\"              # location of WinRAR
LOC_7Z     = "C:\\Program Files\\7-Zip\\"               # location of 7-Zip
SAVE_MODE  = True                                       # if the suffix of file doesn't look like a compressed file, then do nothing with it.

#  you can change it


PROGRAM_RAR  = "UnRAR.exe" # the program we use
PROGRAM_7Z   = "7z.exe"    # the program we use
LOC_S_WINRAR = ["C:\\Program Files\\WinRAR\\","C:\\Program Files (x86)\\WinRAR\\","./",""] # some possible locations of WinRAR
LOC_S_7Z     = ["C:\\Program Files\\7-Zip\\","C:\\Program Files (x86)\\7-Zip\\","./",""]   # some possible locations of 7-Zip
RAR_FILE     = ["rar","zip","7z","tar","gz","xz","bzip2","gzip","wim","arj","cab","chm","cpio","cramfs","deb","dmg","fat","hfs","iso","lzh","lzma","mbr","msi","nsis","ntfs","rpm","squashfs","udf","vhd","xar","z"]
NOT_RAR_FILE = ["jpg","exe","png","mkv","mp4","mp3","avi","mov","jpeg","wav","gif","mpeg","webp","txt","doc","docx","ppt","pptx","xls","xlsx","html","wps","torrent","swf","bmp","crdownload","xltd","downloading"]
ENABLE_RAR = False         # initial state only
ENABLE_7Z = False          # initial state only

# for guessing >>>
GUESS_FLAG_INIT     = ["密碼", "碼", "password", "Password"]    #0
GUESS_FLAG_START_1  = [":", ":"]                            #1
GUESS_FLAG_START_2  = ["是", "為", "is", "are"," "]          #1
GUESS_FLAG_END      = ["\n","   "]                           #2
GUESS_FLAG_DIVIDE   = ["或是", "或", " or "]                 #3
#  for guessing


def guessWDComment(comment):
    guess_flag = 0
    guess_wd: list[str] = []
    guess_ps = 0
    cutIn = 0
    cutOut = 0
    while True:
        if guess_flag == 0:
            guess_newPs = len(comment)
            guess_len = 0
            for initStr in GUESS_FLAG_INIT:
                ps_temp = comment.find(initStr, guess_ps)
                if ps_temp == -1:
                    continue
                else:
                    if ps_tempguess_newPs:
                        guess_newPs = ps_temp
                        guess_len = len(initStr)
            if guess_newPs == len(comment):
                if not guess_wd:
                    cutIn = 0
                    cutOut = len(comment)
                    guess_flag = 3
                else:
                    break
            else:
                guess_ps = guess_newPs + guess_len
                guess_flag = 1
        elif guess_flag == 1:
            found_temp = False
            found_temp_2 = False
            guess_newPs = len(comment)
            for startStr in GUESS_FLAG_START_1:
                ps_temp = comment.find(startStr, guess_ps, guess_ps + 20)
                if ps_temp == -1:
                    continue
                else:
                    if ps_temp  guess_newPs:
                        found_temp = True
                        guess_newPs = ps_temp + len(startStr)
                        guess_flag = 2
            if found_temp:
                guess_ps = guess_newPs
                cutIn = guess_ps
                continue
            else:
                guess_newPs = len(comment)
                for startStr in GUESS_FLAG_START_2:
                    ps_temp = comment.find(startStr, guess_ps, guess_ps + 20)
                    if ps_temp == -1:
                        continue
                    else:
                        if ps_temp  guess_newPs:
                            found_temp_2 = True
                            guess_newPs = ps_temp + len(startStr)
                            guess_flag = 2
            if found_temp_2:
                guess_ps = guess_newPs
            cutIn = guess_ps
            guess_flag = 2
        elif guess_flag == 2:
            guess_newPs = len(comment)
            for endStr in GUESS_FLAG_END:
                ps_temp = comment.find(endStr, guess_ps)
                if ps_temp == -1:
                    continue
                else:
                    if ps_temp  guess_newPs:
                        guess_newPs = ps_temp
            guess_ps = guess_newPs
            guess_flag = 3
            cutOut = guess_ps
        elif guess_flag == 3:
            found_cut_temp = False
            for divideStr in GUESS_FLAG_DIVIDE:
                if comment.find(divideStr, cutIn, cutOut) != -1:
                    found_cut_temp = True
                    for wd in comment[cutIn:cutOut].split(divideStr):
                        guess_wd.append(wd.strip())
                    break
            if not found_cut_temp:
                guess_wd.append(comment[cutIn:cutOut].strip())
            guess_flag = 0
        else:
            guess_flag = 0
    return guess_wd


def isCompressedFile(file):
    file = file.lower()
    for rar in RAR_FILE:
        if file.endswith("." + rar):
            return True
    for media in NOT_RAR_FILE:
        if file.endswith("." + media):
            return False
    return not SAVE_MODE


def utfIsNumber(uchar):
    return uchar >= u'\u0030' and uchar=u'\u0039'


def winRarDo(folder, file, wd):
    extractStr = " x -y -p" + wd + " \"" + folder + "\\" + file + "\" \"" + folder + "\\\""
    extM = subprocess.call("@\""+LOC_WINRAR+PROGRAM_RAR+"\""+extractStr,shell=True)     
    if extM == 1:    # not rar file
        return 2
    elif extM == 11: # wrong password
        return 1
    elif extM != 0:  # error
        return 1
    else:
        return 0


def z7Do(folder, file, wd):
    extractStr = " x -y -p" + wd + " \"" + folder + "\\" + file + "\" -o\"" + folder + "\\\"" 
    extM = subprocess.call("@\""+LOC_7Z+PROGRAM_7Z+"\""+extractStr,shell=True)
    if extM !=0: # error
        return 1
    else:
        return 0


def unrarFile(folder, file):
    successThisFile = False
    fileNameEncrypted = True
    if not folder:
        cutPos = file.rindex("\\")
        folder = file[:cutPos]
        file = file[cutPos+1:]
        #print(folder)
        #print(file)
    if ENABLE_RAR and file.endswith(".rar"):
        winRarReturn = winRarDo(folder, file, PASSWD[0])
        #print(winRarReturn)
        if winRarReturn == 0:
            #successThisFile = True
            return True
        elif winRarReturn == 2:
            pass
        else:
            getCommentStr = " l -p0 -z" + " \"" + folder + "\\" + file + "\""
            commentNumber = subprocess.call("@\""+LOC_WINRAR+PROGRAM_RAR+"\""+getCommentStr,shell=True)
            #commentNumber = 1
            if commentNumber == 0:
                commentM = subprocess.getstatusoutput("@\""+LOC_WINRAR+PROGRAM_RAR+"\""+getCommentStr)
                if commentM[0] == 0:
                    fileNameEncrypted = False
                    comment = commentM[1][(commentM[1].index("\n\n")+2):commentM[1].index(folder)]
                    comment = comment[0:comment.rindex("\n\n")]
                    #print(comment)
                    if comment:
                        wdArray = guessWDComment(comment)
                        print("Possible passwords:", wdArray)
                        for wd in wdArray:
                            winRarReturn = winRarDo(folder, file, wd)
                            if winRarReturn == 1:
                                continue
                            elif winRarReturn == 0:
                                successThisFile = True
                                break
                            elif winRarReturn == 2:
                                break
                            else:
                                break
            if successThisFile:
                return True
            for index in range(1,len(PASSWD)):
                winRarReturn = winRarDo(folder, file, PASSWD[index])
                if winRarReturn == 1:
                    continue
                elif winRarReturn == 0:
                    successThisFile = True
                    PASSWD[0],PASSWD[index]=PASSWD[index],PASSWD[0]
                    break
                elif winRarReturn == 2:
                    break
                else:
                    break
            
    if not successThisFile:
        if ENABLE_7Z:
            for index in range(len(PASSWD)):
                z7Return = z7Do(folder, file, PASSWD[index])
                if z7Return == 1:
                    continue
                else:
                    successThisFile = True
                    PASSWD[0],PASSWD[index]=PASSWD[index],PASSWD[0]
                    break
                     
    if not successThisFile: 
        print("Failed:"+file)
    return successThisFile


def unrar(folder):
    if os.path.isdir(folder):
        print(folder)
        file_list = os.listdir(folder)
        for file in file_list:
            if os.path.isdir(folder + "/" + file):
                #print(folder +"/"+ file)
                #unrar(folder +"/"+file)
                pass
            else:
                if isCompressedFile(file):
                    if unrarFile(folder, file):
                        if DELETEIT:
                            os.remove(folder + "/" + file)
    else:
        if isCompressedFile(folder):
            if unrarFile("", folder):
                if DELETEIT:
                    os.remove(folder)
                  

if __name__ == '__main__':
    if len(sys.argv) = 1:
        sys.exit(1)
    testRar = os.popen("\""+LOC_WINRAR+PROGRAM_RAR+"\"").read()    
    if not testRar:
       for loc in LOC_S_WINRAR:
           testRar = os.popen("\""+loc+PROGRAM_RAR+"\"").read()
           if testRar:
               LOC_WINRAR = loc
               ENABLE_RAR = True
               break
    else:
        ENABLE_RAR = True

    test7z = os.popen("\""+LOC_7Z+PROGRAM_7Z+"\"").read()    
    if not test7z:
       for loc in LOC_S_7Z:
           test7z = os.popen("\""+loc+PROGRAM_7Z+"\"").read()
           if test7z:
               LOC_7Z = loc
               ENABLE_7Z = True
               break
    else:
        ENABLE_7Z = True

    if (not ENABLE_RAR) and (not ENABLE_7Z):
        print("Cannot find winRAR and 7-zip")
        sys.exit(1)
    while len(PASSWD)  2:
        PASSWD.append("0")   
    for folder in sys.argv[1:]:
        #print(folder)
        unrar(folder)
    print("Finish.")
    #subprocess.call("pause",shell=True)
    sys.exit(0)

以上就是用python批量解壓帶密碼的壓縮包的詳細(xì)內(nèi)容,更多關(guān)于python批量解壓壓縮包的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

您可能感興趣的文章:
  • 使用 Python 破解壓縮文件的密碼的思路詳解
  • Python3壓縮和解壓縮實(shí)現(xiàn)代碼
  • python解壓zip包中文亂碼解決方法
  • Python在后臺(tái)自動(dòng)解壓各種壓縮文件的實(shí)現(xiàn)方法
  • python 實(shí)現(xiàn)壓縮和解壓縮的示例
  • Python實(shí)現(xiàn)加密的RAR文件解壓的方法(密碼已知)
  • Python實(shí)現(xiàn)文件壓縮和解壓的示例代碼
  • Python腳本破解壓縮文件口令實(shí)例教程(zipfile)
  • 文件上傳服務(wù)器-jupyter 中python解壓及壓縮方式
  • Python解壓 rar、zip、tar文件的方法
  • python批量解壓zip文件的方法

標(biāo)簽:云南 龍巖 酒泉 江蘇 定西 商丘 寧夏 金融催收

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《用python批量解壓帶密碼的壓縮包》,本文關(guān)鍵詞  用,python,批量,解壓,帶,密碼,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《用python批量解壓帶密碼的壓縮包》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于用python批量解壓帶密碼的壓縮包的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    昌吉市| 南靖县| 科尔| 沁水县| 新邵县| 大足县| 澄城县| 广灵县| 革吉县| 渭南市| 长沙县| 于都县| 镇宁| 宁强县| 龙口市| 玛多县| 郯城县| 青阳县| 新昌县| 红河县| 庄河市| 上蔡县| 云和县| 茌平县| 四会市| 横山县| 韶关市| 连江县| 遂川县| 枝江市| 阿拉善右旗| 岳西县| 鲁山县| 连江县| 东乡族自治县| 双江| 鹰潭市| 石楼县| 阿克苏市| 博罗县| 丹寨县|