compress.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import glob
  2. import json
  3. import os
  4. import re
  5. import subprocess
  6. import sys
  7. import requests
  8. import platform
  9. import shutil
  10. import zipfile
  11. import urllib.request
  12. import py7zr
  13. def compress_folder_to_7z(folder_path, output_file):
  14. if os.path.exists(output_file):
  15. os.remove(output_file)
  16. # with py7zr.SevenZipFile(output_file, 'w') as archive:
  17. # archive.writeall(folder_path, output_file)
  18. # 压缩文件夹
  19. try:
  20. subprocess.call(["7z", "a", output_file, folder_path])
  21. except:
  22. subprocess.call(["7za", "a", output_file, folder_path])
  23. def compress_folder_to_7z_split(folder_path, output_file):
  24. if os.path.exists(output_file):
  25. os.remove(output_file)
  26. file_name = os.path.basename(output_file)
  27. file_dir = os.path.dirname(output_file)
  28. # 获取文件名的前缀
  29. file_prefix = os.path.splitext(file_name)[0]
  30. # 构建分卷文件的路径模式
  31. split_file_pattern = os.path.join(file_dir, file_prefix + ".7z.*")
  32. # 获取匹配的分卷文件列表
  33. split_files = glob.glob(split_file_pattern)
  34. # 删除分卷文件
  35. for split_file in split_files:
  36. os.remove(split_file)
  37. # 压缩文件夹
  38. try:
  39. subprocess.call(["7z", "a", "-v95m", output_file, folder_path])
  40. except:
  41. try:
  42. subprocess.call(["7za", "a", "-v95m", output_file, folder_path])
  43. except:
  44. subprocess.call(["7zz", "a", "-v95m", output_file, folder_path])
  45. easyspider_version = "0.3.5"
  46. if __name__ == "__main__":
  47. if sys.platform == "win32" and platform.architecture()[0] == "64bit":
  48. file_name = f"EasySpider_{easyspider_version}_windows_x64.7z"
  49. if os.path.exists("./EasySpider_windows_x64/user_data"):
  50. shutil.rmtree("./EasySpider_windows_x64/user_data")
  51. if os.path.exists("./EasySpider_windows_x64/Data"):
  52. shutil.rmtree("./EasySpider_windows_x64/Data")
  53. if os.path.exists("./EasySpider_windows_x64/execution_instances"):
  54. shutil.rmtree("./EasySpider_windows_x64/execution_instances")
  55. if os.path.exists("./EasySpider_windows_x64/config.json"):
  56. os.remove("./EasySpider_windows_x64/config.json")
  57. if os.path.exists("./EasySpider_windows_x64/mysql_config.json"):
  58. os.remove("./EasySpider_windows_x64/mysql_config.json")
  59. os.mkdir("./EasySpider_windows_x64/Data")
  60. os.mkdir("./EasySpider_windows_x64/execution_instances")
  61. compress_folder_to_7z_split("./EasySpider_windows_x64", file_name)
  62. print(f"Compress {file_name} Split successfully!")
  63. compress_folder_to_7z("./EasySpider_windows_x64", file_name)
  64. print(f"Compress {file_name} successfully!")
  65. elif sys.platform == "win32" and platform.architecture()[0] == "32bit":
  66. file_name = f"EasySpider_{easyspider_version}_windows_x86.7z"
  67. if os.path.exists("./EasySpider_windows_x86/user_data"):
  68. shutil.rmtree("./EasySpider_windows_x86/user_data")
  69. if os.path.exists("./EasySpider_windows_x86/Data"):
  70. shutil.rmtree("./EasySpider_windows_x86/Data")
  71. if os.path.exists("./EasySpider_windows_x86/execution_instances"):
  72. shutil.rmtree("./EasySpider_windows_x86/execution_instances")
  73. if os.path.exists("./EasySpider_windows_x86/config.json"):
  74. os.remove("./EasySpider_windows_x86/config.json")
  75. if os.path.exists("./EasySpider_windows_x86/mysql_config.json"):
  76. os.remove("./EasySpider_windows_x86/mysql_config.json")
  77. os.mkdir("./EasySpider_windows_x86/Data")
  78. os.mkdir("./EasySpider_windows_x86/execution_instances")
  79. compress_folder_to_7z_split("./EasySpider_windows_x86", file_name)
  80. print(f"Compress {file_name} Split successfully!")
  81. compress_folder_to_7z("./EasySpider_windows_x86", file_name)
  82. print(f"Compress {file_name} successfully!")
  83. elif sys.platform == "linux" and platform.architecture()[0] == "64bit":
  84. file_name = f"EasySpider_{easyspider_version}_Linux_x64.tar.xz"
  85. if os.path.exists("./EasySpider_Linux_x64/user_data"):
  86. shutil.rmtree("./EasySpider_Linux_x64/user_data")
  87. if os.path.exists("./EasySpider_Linux_x64/Data"):
  88. shutil.rmtree("./EasySpider_Linux_x64/Data")
  89. if os.path.exists("./EasySpider_Linux_x64/execution_instances"):
  90. shutil.rmtree("./EasySpider_Linux_x64/execution_instances")
  91. if os.path.exists("./EasySpider_Linux_x64/config.json"):
  92. os.remove("./EasySpider_Linux_x64/config.json")
  93. if os.path.exists("./EasySpider_Linux_x64/mysql_config.json"):
  94. os.remove("./EasySpider_Linux_x64/mysql_config.json")
  95. os.mkdir("./EasySpider_Linux_x64/Data")
  96. os.mkdir("./EasySpider_Linux_x64/execution_instances")
  97. subprocess.call(["tar", "-Jcvf", file_name, "./EasySpider_Linux_x64"])
  98. print(f"Compress {file_name} successfully!")
  99. elif sys.platform == "darwin" and platform.architecture()[0] == "64bit":
  100. file_name = f"EasySpider_{easyspider_version}_MacOS_all_arch.tar.gz"
  101. if os.path.exists("./EasySpider_MacOS_all_arch/Data"):
  102. shutil.rmtree("./EasySpider_MacOS_all_arch/Data")
  103. os.mkdir("./EasySpider_MacOS_all_arch/Data")
  104. subprocess.call(["tar", "-zcvf", file_name, "./EasySpider_MacOS_all_arch"])
  105. subprocess.call(["7zz", "a", "-v95m", file_name.replace(".tar.gz", ".7z"), file_name, "请继续解压EasySpider_MacOS_all_arch.tar.gz使用.txt"])
  106. print(f"Compress {file_name} successfully!")