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

2021-06-15 14:04

阅读:538

标签:mkdir   博客   none   exists   dict   int   wal   eve   extra   

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

给你个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包中的文件按大小排序

标签:mkdir   博客   none   exists   dict   int   wal   eve   extra   

原文地址:https://www.cnblogs.com/LanTianYou/p/9732605.html


评论


亲,登录后才可以留言!