经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Python » 查看文章
用python批量解压带密码的压缩包
来源:jb51  时间:2021/5/31 18:23:26  对本文有异议

项目地址:

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

环境需求

  • Windows系统
  • Python 3
  • 对于解压RAR文件,需要安装WinRAR
  • 对于解压7z/zip等其他7-Zip支持解压的文件,需要安装7-Zip

用法 Usage

直接拖入文件夹或压缩文件即可批量解压缩包含密码的压缩文件。如果拖入的是文件夹,则会把该文件夹下的压缩文件解压缩,但不进入下一级目录。通过设置PASSWD来设置字典,通过设置DELETEIT来设置解压后是否删除被成功解压的压缩文件。本脚本会通过文件的后缀识别该文件是否为压缩文件。

你可以把WinRAR目录下的Unrar.exe和7-Zip目录下的7z.exe直接复制到这个toolUnRar.py文件的相同目录下,这样就可以携带使用了。

参数 Parameters

  • PASSWD = ["hello","123456"] :你的密码本,该脚本会从这个数组中不断试验密码来解压缩,直到成功为止。
  • DELETEIT :一个危险的参数。为真时,该脚本会直接删除成功解压的压缩文件。为假则不会删除。
  • LOC_WINRAR = "C:\Program Files\WinRAR\" 你的WinRAR安装位置。就算这个变量的设置的不对,该程序也会在可能的位置来寻找对应的程序。
  • LOC_7Z:7-Zip的安装位置。
  • SAVE_MODE = True:如果该脚本无法通过后缀判断这是不是压缩文件,则不对该文件进行操作。

完整代码

  1. #!/usr/bin/python3
  2. # -*- coding: UTF-8 -*-
  3.  
  4. # Created by Mario Chen, 04.04.2021, Shenzhen
  5. # My Github site: https://github.com/Mario-Hero
  6.  
  7. import sys
  8. import os
  9. import subprocess
  10.  
  11. # you can change it >>>>>
  12.  
  13. PASSWD = ["123456","hello"] # the possible passwords
  14. DELETEIT = False # DANGER!! If it is True,will delete rar file after extraction
  15. LOC_WINRAR = "C:\\Program Files\\WinRAR\\" # location of WinRAR
  16. LOC_7Z = "C:\\Program Files\\7-Zip\\" # location of 7-Zip
  17. SAVE_MODE = True # if the suffix of file doesn't look like a compressed file, then do nothing with it.
  18.  
  19. # <<<<< you can change it
  20.  
  21.  
  22. PROGRAM_RAR = "UnRAR.exe" # the program we use
  23. PROGRAM_7Z = "7z.exe" # the program we use
  24. LOC_S_WINRAR = ["C:\\Program Files\\WinRAR\\","C:\\Program Files (x86)\\WinRAR\\","./",""] # some possible locations of WinRAR
  25. LOC_S_7Z = ["C:\\Program Files\\7-Zip\\","C:\\Program Files (x86)\\7-Zip\\","./",""] # some possible locations of 7-Zip
  26. 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"]
  27. 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"]
  28. ENABLE_RAR = False # initial state only
  29. ENABLE_7Z = False # initial state only
  30.  
  31. # for guessing >>>
  32. GUESS_FLAG_INIT = ["密码", "码", "password", "Password"] #0
  33. GUESS_FLAG_START_1 = [":", ":"] #1
  34. GUESS_FLAG_START_2 = ["是", "为", "is", "are"," "] #1
  35. GUESS_FLAG_END = ["\n"," "] #2
  36. GUESS_FLAG_DIVIDE = ["或是", "或", " or "] #3
  37. # <<< for guessing
  38.  
  39.  
  40. def guessWDComment(comment):
  41. guess_flag = 0
  42. guess_wd: list[str] = []
  43. guess_ps = 0
  44. cutIn = 0
  45. cutOut = 0
  46. while True:
  47. if guess_flag == 0:
  48. guess_newPs = len(comment)
  49. guess_len = 0
  50. for initStr in GUESS_FLAG_INIT:
  51. ps_temp = comment.find(initStr, guess_ps)
  52. if ps_temp == -1:
  53. continue
  54. else:
  55. if ps_temp<guess_newPs:
  56. guess_newPs = ps_temp
  57. guess_len = len(initStr)
  58. if guess_newPs == len(comment):
  59. if not guess_wd:
  60. cutIn = 0
  61. cutOut = len(comment)
  62. guess_flag = 3
  63. else:
  64. break
  65. else:
  66. guess_ps = guess_newPs + guess_len
  67. guess_flag = 1
  68. elif guess_flag == 1:
  69. found_temp = False
  70. found_temp_2 = False
  71. guess_newPs = len(comment)
  72. for startStr in GUESS_FLAG_START_1:
  73. ps_temp = comment.find(startStr, guess_ps, guess_ps + 20)
  74. if ps_temp == -1:
  75. continue
  76. else:
  77. if ps_temp < guess_newPs:
  78. found_temp = True
  79. guess_newPs = ps_temp + len(startStr)
  80. guess_flag = 2
  81. if found_temp:
  82. guess_ps = guess_newPs
  83. cutIn = guess_ps
  84. continue
  85. else:
  86. guess_newPs = len(comment)
  87. for startStr in GUESS_FLAG_START_2:
  88. ps_temp = comment.find(startStr, guess_ps, guess_ps + 20)
  89. if ps_temp == -1:
  90. continue
  91. else:
  92. if ps_temp < guess_newPs:
  93. found_temp_2 = True
  94. guess_newPs = ps_temp + len(startStr)
  95. guess_flag = 2
  96. if found_temp_2:
  97. guess_ps = guess_newPs
  98. cutIn = guess_ps
  99. guess_flag = 2
  100. elif guess_flag == 2:
  101. guess_newPs = len(comment)
  102. for endStr in GUESS_FLAG_END:
  103. ps_temp = comment.find(endStr, guess_ps)
  104. if ps_temp == -1:
  105. continue
  106. else:
  107. if ps_temp < guess_newPs:
  108. guess_newPs = ps_temp
  109. guess_ps = guess_newPs
  110. guess_flag = 3
  111. cutOut = guess_ps
  112. elif guess_flag == 3:
  113. found_cut_temp = False
  114. for divideStr in GUESS_FLAG_DIVIDE:
  115. if comment.find(divideStr, cutIn, cutOut) != -1:
  116. found_cut_temp = True
  117. for wd in comment[cutIn:cutOut].split(divideStr):
  118. guess_wd.append(wd.strip())
  119. break
  120. if not found_cut_temp:
  121. guess_wd.append(comment[cutIn:cutOut].strip())
  122. guess_flag = 0
  123. else:
  124. guess_flag = 0
  125. return guess_wd
  126.  
  127.  
  128. def isCompressedFile(file):
  129. file = file.lower()
  130. for rar in RAR_FILE:
  131. if file.endswith("." + rar):
  132. return True
  133. for media in NOT_RAR_FILE:
  134. if file.endswith("." + media):
  135. return False
  136. return not SAVE_MODE
  137.  
  138.  
  139. def utfIsNumber(uchar):
  140. return uchar >= u'\u0030' and uchar<=u'\u0039'
  141.  
  142.  
  143. def winRarDo(folder, file, wd):
  144. extractStr = " x -y -p" + wd + " \"" + folder + "\\" + file + "\" \"" + folder + "\\\""
  145. extM = subprocess.call("@\""+LOC_WINRAR+PROGRAM_RAR+"\""+extractStr,shell=True)
  146. if extM == 1: # not rar file
  147. return 2
  148. elif extM == 11: # wrong password
  149. return 1
  150. elif extM != 0: # error
  151. return 1
  152. else:
  153. return 0
  154.  
  155.  
  156. def z7Do(folder, file, wd):
  157. extractStr = " x -y -p" + wd + " \"" + folder + "\\" + file + "\" -o\"" + folder + "\\\""
  158. extM = subprocess.call("@\""+LOC_7Z+PROGRAM_7Z+"\""+extractStr,shell=True)
  159. if extM !=0: # error
  160. return 1
  161. else:
  162. return 0
  163.  
  164.  
  165. def unrarFile(folder, file):
  166. successThisFile = False
  167. fileNameEncrypted = True
  168. if not folder:
  169. cutPos = file.rindex("\\")
  170. folder = file[:cutPos]
  171. file = file[cutPos+1:]
  172. #print(folder)
  173. #print(file)
  174. if ENABLE_RAR and file.endswith(".rar"):
  175. winRarReturn = winRarDo(folder, file, PASSWD[0])
  176. #print(winRarReturn)
  177. if winRarReturn == 0:
  178. #successThisFile = True
  179. return True
  180. elif winRarReturn == 2:
  181. pass
  182. else:
  183. getCommentStr = " l -p0 -z" + " \"" + folder + "\\" + file + "\""
  184. commentNumber = subprocess.call("@\""+LOC_WINRAR+PROGRAM_RAR+"\""+getCommentStr,shell=True)
  185. #commentNumber = 1
  186. if commentNumber == 0:
  187. commentM = subprocess.getstatusoutput("@\""+LOC_WINRAR+PROGRAM_RAR+"\""+getCommentStr)
  188. if commentM[0] == 0:
  189. fileNameEncrypted = False
  190. comment = commentM[1][(commentM[1].index("\n\n")+2):commentM[1].index(folder)]
  191. comment = comment[0:comment.rindex("\n\n")]
  192. #print(comment)
  193. if comment:
  194. wdArray = guessWDComment(comment)
  195. print("Possible passwords:", wdArray)
  196. for wd in wdArray:
  197. winRarReturn = winRarDo(folder, file, wd)
  198. if winRarReturn == 1:
  199. continue
  200. elif winRarReturn == 0:
  201. successThisFile = True
  202. break
  203. elif winRarReturn == 2:
  204. break
  205. else:
  206. break
  207. if successThisFile:
  208. return True
  209. for index in range(1,len(PASSWD)):
  210. winRarReturn = winRarDo(folder, file, PASSWD[index])
  211. if winRarReturn == 1:
  212. continue
  213. elif winRarReturn == 0:
  214. successThisFile = True
  215. PASSWD[0],PASSWD[index]=PASSWD[index],PASSWD[0]
  216. break
  217. elif winRarReturn == 2:
  218. break
  219. else:
  220. break
  221. if not successThisFile:
  222. if ENABLE_7Z:
  223. for index in range(len(PASSWD)):
  224. z7Return = z7Do(folder, file, PASSWD[index])
  225. if z7Return == 1:
  226. continue
  227. else:
  228. successThisFile = True
  229. PASSWD[0],PASSWD[index]=PASSWD[index],PASSWD[0]
  230. break
  231. if not successThisFile:
  232. print("Failed:"+file)
  233. return successThisFile
  234.  
  235.  
  236. def unrar(folder):
  237. if os.path.isdir(folder):
  238. print(folder)
  239. file_list = os.listdir(folder)
  240. for file in file_list:
  241. if os.path.isdir(folder + "/" + file):
  242. #print(folder +"/"+ file)
  243. #unrar(folder +"/"+file)
  244. pass
  245. else:
  246. if isCompressedFile(file):
  247. if unrarFile(folder, file):
  248. if DELETEIT:
  249. os.remove(folder + "/" + file)
  250. else:
  251. if isCompressedFile(folder):
  252. if unrarFile("", folder):
  253. if DELETEIT:
  254. os.remove(folder)
  255.  
  256. if __name__ == '__main__':
  257. if len(sys.argv) <= 1:
  258. sys.exit(1)
  259. testRar = os.popen("\""+LOC_WINRAR+PROGRAM_RAR+"\"").read()
  260. if not testRar:
  261. for loc in LOC_S_WINRAR:
  262. testRar = os.popen("\""+loc+PROGRAM_RAR+"\"").read()
  263. if testRar:
  264. LOC_WINRAR = loc
  265. ENABLE_RAR = True
  266. break
  267. else:
  268. ENABLE_RAR = True
  269.  
  270. test7z = os.popen("\""+LOC_7Z+PROGRAM_7Z+"\"").read()
  271. if not test7z:
  272. for loc in LOC_S_7Z:
  273. test7z = os.popen("\""+loc+PROGRAM_7Z+"\"").read()
  274. if test7z:
  275. LOC_7Z = loc
  276. ENABLE_7Z = True
  277. break
  278. else:
  279. ENABLE_7Z = True
  280.  
  281. if (not ENABLE_RAR) and (not ENABLE_7Z):
  282. print("Cannot find winRAR and 7-zip")
  283. sys.exit(1)
  284. while len(PASSWD) < 2:
  285. PASSWD.append("0")
  286. for folder in sys.argv[1:]:
  287. #print(folder)
  288. unrar(folder)
  289. print("Finish.")
  290. #subprocess.call("pause",shell=True)
  291. sys.exit(0)

以上就是用python批量解压带密码的压缩包的详细内容,更多关于python批量解压压缩包的资料请关注w3xue其它相关文章!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号