def replace(): raw = json.dumps(bson_data) #raw raw = raw.replace('\\','') #replace \ raw = raw.replace('": "{"','": {"') #replace front " raw = raw.replace('"}",','"},') #replace back " raw = raw.replace('"}"},','"}},').replace('}"}}','}}}') #replace special " raw = raw.replace('":.','":0.').replace('":-.','":-0.') #add 0. return raw
raw = replace()
with open('charAssets.json','w') as f: f.write(raw)
def unzip(self,bundle_path,dir_path): """解压CharAsset.zip\n :param bundle_path: 压缩包地址\n :param dir_path: 解压路径文件夹""" zip_file = zipfile.ZipFile(bundle_path) zip_ls = zip_file.namelist() for f in zip_ls: zip_file.extract(f, dir_path) zip_file.close()
def bson2json(self,bundle_path,dir_path,dst_path): """批量转换bson文件为json文件\n :param bundle_path: 压缩包地址\n :param dir_path: 解压路径文件夹\n :param dst_path: json文件输出路径文件夹 """ if os.path.isfile(os.path.join(dir_path,'Manifest.bson')) != True: self.unzip(bundle_path,dir_path) for root,dirs,files in os.walk(dir_path): for file_name in files: file_path = os.path.join(root,file_name) data = gzip.GzipFile(file_path,'rb').read().decode() if os.path.isdir(dst_path) != True: os.mkdir(dst_path) if os.path.isfile(os.path.join(dst_path,'Manifest.json')) != True: with open(os.path.join(dst_path,file_name).replace('bson','json'),'w',encoding='utf8') as f: f.write(data) f.close() del dirs