1. 程式人生 > >Python3將ipa包中的文件按大小排序

Python3將ipa包中的文件按大小排序

unzip otf files sts urn root rev fix opd

[本文出自天外歸雲的博客園]

給你個ipa包,解壓前輸出包大小,解壓後把裏面的文件按大小排序。代碼如下:

import os
import shutil
import zipfile

_ipa_zip_path = lambda ipa_path: ipa_path.replace(.ipa, .zip)
_file_size = lambda file_path: os.path.getsize(file_path) / 1024 / 1024


def unzip(zip_path: str) -> str:
    dir_path = None
    
if zip_path.endswith(.zip): print(f{zip_path} file size:{round(_file_size(zip_path),3)}mb) zip_name = os.path.basename(zip_path) dir_name = zip_name.replace(.zip, ‘‘) dir_root_path = zip_path.replace(zip_name, ‘‘) dir_path = os.path.join(dir_root_path, dir_name)
if os.path.exists(dir_path): shutil.rmtree(dir_path) os.mkdir(dir_path) zip_file = zipfile.ZipFile(zip_path) for file_name in zip_file.namelist(): zip_file.extract(file_name, dir_path) zip_file.close() return dir_path def rename_suffix(raw, raw_type, target) -> None:
if raw.endswith(raw_type) and os.path.exists(raw): os.rename(raw, target) def walk_files(dir_path) -> list: file_dicts = [] if os.path.exists(dir_path): for root, dirs, files in os.walk(dir_path, topdown=True): for name in files: file_path = os.path.join(root, name) file_dict = { file_name: name, file_size: round(_file_size(file_path), 8), } file_dicts.append(file_dict) return file_dicts def show_files_size(dir_path=None) -> None: if dir_path: file_dicts_sorted = sorted(walk_files(dir_path), key=lambda e: (e.__getitem__(file_size), e.__getitem__(file_name)), reverse=True) for file_dict in file_dicts_sorted: print(f{file_dict["file_name"]}->{file_dict["file_size"]}mb) def ipa_checker(ipa_path: str) -> None: try: ipa_file_size = _file_size(ipa_path) print(f{ipa_path} file size:{round(ipa_file_size,3)}mb) except FileNotFoundError as error: print(fFile not exists->{ipa_path}) ipa_zip_path = _ipa_zip_path(ipa_path) rename_suffix(ipa_path, .ipa, ipa_zip_path) try: dir_path = unzip(ipa_zip_path) show_files_size(dir_path) except OSError as error: print(error) if __name__ == __main__: ipa_path = rC:\Users\kkk\Desktop\xxx.ipa ipa_checker(ipa_path)

哦了。

Python3將ipa包中的文件按大小排序