博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python3将ipa包中的文件按大小排序
阅读量:7229 次
发布时间:2019-06-29

本文共 2349 字,大约阅读时间需要 7 分钟。

[本文出自天外归云的博客园]

给你个ipa包,解压前输出包大小,解压后把里面的文件按大小排序。代码如下:

import osimport shutilimport zipfile_ipa_zip_path = lambda ipa_path: ipa_path.replace('.ipa', '.zip')_file_size = lambda file_path: os.path.getsize(file_path) / 1024 / 1024def 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_pathdef 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_dictsdef 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(f'File 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 = r'C:\Users\kkk\Desktop\xxx.ipa'    ipa_checker(ipa_path)

哦了。

转载地址:http://dxdfm.baihongyu.com/

你可能感兴趣的文章
了解webpack-4.0版本(一)
查看>>
如何培养良好的编程风格
查看>>
Netty Channel源码分析
查看>>
基于 HTML5 WebGL 的 3D 机房
查看>>
Java编程——数据库两大神器:索引和锁
查看>>
springMvc学习笔记(2)
查看>>
吐槽Javascript系列二:数组中的splice和slice方法
查看>>
什么是Javascript函数节流?
查看>>
MQ框架的比较
查看>>
oschina
查看>>
Octave 入门
查看>>
深度学习入门:10门免费线上课程推荐
查看>>
React组件设计模式(一)
查看>>
E-HPC支持多队列管理和自动伸缩
查看>>
express + mock 让前后台并行开发
查看>>
30天自制操作系统-2
查看>>
小程序开发之路(一)
查看>>
Odoo domain写法及运用
查看>>
JavaScript工作原理(五):深入了解WebSockets,HTTP/2和SSE,以及如何选择
查看>>
猫头鹰的深夜翻译:Java 2D Graphics, 简单的仿射变换
查看>>