split_zip.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # -*- coding: utf-8 -*-
  2. #按照大小分割文件
  3. import os
  4. import sys
  5. import zipfile
  6. import random
  7. py_path = sys.argv[0][::-1].split('/',1)[1][::-1]+'/'
  8. os.chdir(py_path)
  9. #示例 python split_zip.py fused_games.zip aab_fix. ../res 1024
  10. #
  11. split_file_path = sys.argv[1]
  12. out_file_prefix = sys.argv[2]
  13. out_file_path = sys.argv[3]
  14. size = int(sys.argv[4]) * 1024 #分割大小
  15. class_files_list = []
  16. class_key_list = ["Class","Def","Layer","Unit","Component","Pic","Manager","Data"]
  17. with open('FileNames') as file:
  18. content = file.read()
  19. class_files_list = content.split("\n")
  20. def zip_dir(path, ziph):
  21. # ziph is zipfile handle
  22. for root, dirs, files in os.walk(path):
  23. for file in files:
  24. print(os.path.join(root, file))
  25. ziph.write(os.path.join(root, file),
  26. os.path.relpath(os.path.join(root, file),
  27. os.path.join(path, '..')))
  28. def zip_it(zip_name):
  29. dir_list = ["../../src","../../res"]
  30. zipf = zipfile.ZipFile(zip_name, 'w', zipfile.ZIP_DEFLATED)
  31. for dir in dir_list:
  32. zip_dir(dir, zipf)
  33. zipf.close()
  34. def del_files(dir_path):
  35. for root, dirs, files in os.walk(dir_path, topdown=False):
  36. for name in files:
  37. os.remove(os.path.join(root, name)) # 删除文件
  38. for name in dirs:
  39. os.rmdir(os.path.join(root, name)) # 删除一个空目录
  40. def mk_sub_file(file_prefix,sub,buf):
  41. [des_split_file_path, extname] = os.path.splitext(file_prefix)
  42. if sub != 0:
  43. pass
  44. split_file_path = out_file_path + '/' + des_split_file_path + '_' + str(sub) + extname
  45. with open(split_file_path,'wb') as fout:
  46. fout.write(buf)
  47. return sub+1
  48. def split(split_file_path,size):
  49. if not os.path.exists(out_file_path):
  50. os.mkdir(out_file_path)
  51. else:
  52. del_files(out_file_path)
  53. with open(split_file_path,'rb') as fin:
  54. read_size = size + (random.randint(100, 500) * 1024)
  55. buf = fin.read(read_size)
  56. sub = 0
  57. while len(buf)>0:
  58. sub = mk_sub_file(out_file_prefix,sub,buf)
  59. read_size = size + (random.randint(100, 500) * 1024)
  60. buf = fin.read(read_size)
  61. mk_sub_file(out_file_prefix,sub,buf)
  62. for x in xrange(1,random.randint(20,40)):
  63. lenght = len(class_files_list)
  64. class_name = class_files_list[random.randint(0,lenght)]
  65. key = class_key_list[random.randint(0,len(class_key_list) - 1)]
  66. rep_str = class_name[random.randint(0,len(class_name) - 1)]
  67. class_name = class_name.replace(rep_str,key)
  68. repeat_times = random.randint(20, 30)
  69. write_content = ""
  70. for i in range(repeat_times):
  71. write_content = write_content + class_name + key + rep_str + class_name
  72. mk_sub_file(class_name,random.randint(0,sub),write_content)
  73. pass
  74. print("ok")
  75. if __name__=="__main__":
  76. zip_it("fused_games.zip")
  77. #del_files("../../res")
  78. #del_files("../../src")
  79. split(split_file_path, size)
  80. del_files("fused_games.zip")
  81. with open(out_file_path + "/gamezipres.tag", 'w') as f:
  82. print(f)