convert_rar.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import os
  2. import sys
  3. sys.path.append(os.path.dirname(__file__) + "/../")
  4. import logging
  5. import traceback
  6. from format_convert import get_memory_info
  7. from format_convert.utils import get_platform, rename_inner_files, judge_error_code, judge_format, slash_replace
  8. @get_memory_info.memory_decorator
  9. def rar2text(path, unique_type_dir):
  10. from format_convert.convert import getText
  11. logging.info("into rar2text")
  12. try:
  13. rar_path = unique_type_dir
  14. try:
  15. # shell调用unrar解压
  16. _signal = os.system("unrar x " + path + " " + rar_path)
  17. print("rar2text _signal", _signal)
  18. # =0, 解压成功
  19. if _signal != 0:
  20. raise Exception
  21. except Exception as e:
  22. logging.info("rar format error!")
  23. print("rar format error!", e)
  24. return [-3]
  25. # 获取文件名
  26. # file_list = []
  27. # for root, dirs, files in os.walk(rar_path, topdown=False):
  28. # for name in dirs:
  29. # file_list.append(os.path.join(root, name) + os.sep)
  30. # for name in files:
  31. # file_list.append(os.path.join(root, name))
  32. if get_platform() == "Windows":
  33. print("============= rar file list")
  34. # 内部文件重命名
  35. file_list = rename_inner_files(rar_path)
  36. if judge_error_code(file_list):
  37. return file_list
  38. text = []
  39. for file in file_list:
  40. if os.path.isdir(file):
  41. continue
  42. # 无文件后缀,猜格式
  43. if len(file.split(".")) <= 1:
  44. logging.info(str(file) + " has no type! Guess type...")
  45. _type = judge_format(file)
  46. if _type is None:
  47. logging.info(str(file) + "cannot guess type!")
  48. sub_text = [""]
  49. else:
  50. logging.info(str(file) + " guess type: " + _type)
  51. new_file = str(file) + "." + _type
  52. os.rename(file, new_file)
  53. file = new_file
  54. sub_text = getText(_type, file)
  55. # 有文件后缀,截取
  56. else:
  57. _type = file.split(".")[-1]
  58. sub_text = getText(_type, file)
  59. if judge_error_code(sub_text, code=[-3]):
  60. continue
  61. if judge_error_code(sub_text):
  62. return sub_text
  63. # print("sub text", sub_text, file, _type)
  64. text = text + sub_text
  65. return text
  66. except Exception as e:
  67. logging.info("rar2text error!")
  68. print("rar2text", traceback.print_exc())
  69. return [-1]