# -*- coding: utf-8 -*- #按照大小分割文件 import os import sys import zipfile import random py_path = sys.argv[0][::-1].split('/',1)[1][::-1]+'/' os.chdir(py_path) #示例 python split_zip.py fused_games.zip aab_fix. ../res 1024 # split_file_path = sys.argv[1] out_file_prefix = sys.argv[2] out_file_path = sys.argv[3] size = int(sys.argv[4]) * 1024 #分割大小 class_files_list = [] class_key_list = ["Class","Def","Layer","Unit","Component","Pic","Manager","Data"] with open('FileNames') as file: content = file.read() class_files_list = content.split("\n") def zip_dir(path, ziph): # ziph is zipfile handle for root, dirs, files in os.walk(path): for file in files: print(os.path.join(root, file)) ziph.write(os.path.join(root, file), os.path.relpath(os.path.join(root, file), os.path.join(path, '..'))) def zip_it(zip_name): dir_list = ["../../src","../../res"] zipf = zipfile.ZipFile(zip_name, 'w', zipfile.ZIP_DEFLATED) for dir in dir_list: zip_dir(dir, zipf) zipf.close() def del_files(dir_path): for root, dirs, files in os.walk(dir_path, topdown=False): for name in files: os.remove(os.path.join(root, name)) # 删除文件 for name in dirs: os.rmdir(os.path.join(root, name)) # 删除一个空目录 def mk_sub_file(file_prefix,sub,buf): [des_split_file_path, extname] = os.path.splitext(file_prefix) if sub != 0: pass split_file_path = out_file_path + '/' + des_split_file_path + '_' + str(sub) + extname with open(split_file_path,'wb') as fout: fout.write(buf) return sub+1 def split(split_file_path,size): if not os.path.exists(out_file_path): os.mkdir(out_file_path) else: del_files(out_file_path) with open(split_file_path,'rb') as fin: read_size = size + (random.randint(100, 500) * 1024) buf = fin.read(read_size) sub = 0 while len(buf)>0: sub = mk_sub_file(out_file_prefix,sub,buf) read_size = size + (random.randint(100, 500) * 1024) buf = fin.read(read_size) mk_sub_file(out_file_prefix,sub,buf) for x in xrange(1,random.randint(20,40)): lenght = len(class_files_list) class_name = class_files_list[random.randint(0,lenght)] key = class_key_list[random.randint(0,len(class_key_list) - 1)] rep_str = class_name[random.randint(0,len(class_name) - 1)] class_name = class_name.replace(rep_str,key) repeat_times = random.randint(20, 30) write_content = "" for i in range(repeat_times): write_content = write_content + class_name + key + rep_str + class_name mk_sub_file(class_name,random.randint(0,sub),write_content) pass print("ok") if __name__=="__main__": zip_it("fused_games.zip") #del_files("../../res") #del_files("../../src") split(split_file_path, size) del_files("fused_games.zip") with open(out_file_path + "/gamezipres.tag", 'w') as f: print(f)