convert_zip.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import os
  2. import sys
  3. sys.path.append(os.path.dirname(__file__) + "/../")
  4. import logging
  5. import traceback
  6. import zipfile
  7. from format_convert import get_memory_info
  8. from format_convert.utils import get_platform, rename_inner_files, judge_error_code, judge_format
  9. @get_memory_info.memory_decorator
  10. def zip2text(path, unique_type_dir):
  11. from format_convert.convert import getText
  12. logging.info("into zip2text")
  13. try:
  14. zip_path = unique_type_dir
  15. try:
  16. zip_file = zipfile.ZipFile(path)
  17. zip_list = zip_file.namelist()
  18. # print("zip list namelist", zip_list)
  19. if get_platform() == "Windows":
  20. if os.path.exists(zip_list[0]):
  21. print("zip2text exists")
  22. # 循环解压文件到指定目录
  23. file_list = []
  24. for f in zip_list:
  25. file_list.append(zip_file.extract(f, path=zip_path))
  26. # zip_file.extractall(path=zip_path)
  27. zip_file.close()
  28. # 获取文件名
  29. # file_list = []
  30. # for root, dirs, files in os.walk(zip_path, topdown=False):
  31. # for name in dirs:
  32. # file_list.append(os.path.join(root, name) + os.sep)
  33. # for name in files:
  34. # file_list.append(os.path.join(root, name))
  35. #
  36. # # if get_platform() == "Windows":
  37. # # print("file_list", file_list)
  38. #
  39. # # 过滤掉doc缓存文件
  40. # temp_list = []
  41. # for f in file_list:
  42. # if re.search("~\$", f):
  43. # continue
  44. # else:
  45. # temp_list.append(f)
  46. # file_list = temp_list
  47. except Exception as e:
  48. logging.info("zip format error!")
  49. print("zip format error!", traceback.print_exc())
  50. return [-3]
  51. # 内部文件重命名
  52. # file_list = inner_file_rename(file_list)
  53. file_list = rename_inner_files(zip_path)
  54. if judge_error_code(file_list):
  55. return file_list
  56. if get_platform() == "Windows":
  57. print("============= zip file list")
  58. # print(file_list)
  59. text = []
  60. for file in file_list:
  61. if os.path.isdir(file):
  62. continue
  63. # 无文件后缀,猜格式
  64. if len(file.split(".")) <= 1:
  65. logging.info(str(file) + " has no type! Guess type...")
  66. _type = judge_format(file)
  67. if _type is None:
  68. logging.info(str(file) + "cannot guess type!")
  69. sub_text = [""]
  70. else:
  71. logging.info(str(file) + " guess type: " + _type)
  72. new_file = str(file) + "." + _type
  73. os.rename(file, new_file)
  74. file = new_file
  75. sub_text = getText(_type, file)
  76. # 有文件后缀,截取
  77. else:
  78. _type = file.split(".")[-1]
  79. sub_text = getText(_type, file)
  80. if judge_error_code(sub_text, code=[-3]):
  81. continue
  82. if judge_error_code(sub_text):
  83. return sub_text
  84. text = text + sub_text
  85. return text
  86. except Exception as e:
  87. logging.info("zip2text error!")
  88. print("zip2text", traceback.print_exc())
  89. return [-1]