计算缓存大小
发布日期:2021-11-22 04:28:51 浏览次数:1 分类:技术文章

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

首先要知道缓存文件的大小,或者缓存文件夹内的文件大小,

获取文件大小

c语言 实现

#include "sys/stat.h"

- (long long) fileSizeAtPath:(NSString*) filePath{  

    struct stat st;  
    if(lstat([filePath cStringUsingEncoding:NSUTF8StringEncoding], &st) == 0){  
        return st.st_size;  
    }  
    return 0;  
}  

objective-c 语言实现

-(long long) fileSizeAtPath:(NSString*) filePath{  

  NSFileManager* manager = [NSFileManager defaultManager];  

  if ([manager fileExistsAtPath:filePath]){  

    return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];  

  }  

  return 0;  

}  

其中C语言的性能比OC要高得多。

获取目录大小

- (long long)folderSize:(const char *)folderPath {    long long folderSize = 0;    DIR* dir = opendir(folderPath);    if (dir == NULL) {         return 0;    }    struct dirent* child;    while ((child = readdir(dir)) != NULL) {        if (child->d_type == DT_DIR             && (child->d_name[0] == '.' && child->d_name[1] == 0)) {            continue;        }        if (child->d_type == DT_DIR             && (child->d_name[0] == '.' && child->d_name[1] == '.' && child->d_name[2] == 0)) {            continue;        }        int folderPathLength = strlen(folderPath);        char childPath[1024];        stpcpy(childPath, folderPath);        if (folderPath[folderPathLength - 1] != '/'){            childPath[folderPathLength] = '/';            folderPathLength++;        }        stpcpy(childPath + folderPathLength, child->d_name);        childPath[folderPathLength + child->d_namlen] = 0;        if (child->d_type == DT_DIR){            folderSize += [self folderSize:childPath];            struct stat st;            if (lstat(childPath, &st) == 0) {                folderSize += st.st_size;            }        } else if (child->d_type == DT_REG || child->d_type == DT_LNK){            struct stat st;            if (lstat(childPath, &st) == 0) {                folderSize += st.st_size;            }        }    }    return folderSize;}

转载地址:https://blog.csdn.net/zxwcool123/article/details/50488969 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:iOS framework 制作教程
下一篇:iOS使用自定义字体

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2024年03月05日 03时59分49秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

rust和gta5哪个吃配置_盘点4款Steam“自由度”很高的游戏,GTA5众所周知,目前最热门... 2019-04-21
es审计日志_elasticsearch 事务日志translog 2019-04-21
dw1510_超低温种子储存柜 2019-04-21
文件未找到mathpage.wll_解决MathPage.wll文件找不到的问题(找了好久的良心之作)... 2019-04-21
java 使用或覆盖了已过时的api_JAVA使用或覆盖了已过时的 API 2019-04-21
java 图片旋转保存_Java 对图片90度旋转 2019-04-21
用java实现文学研究助手_数据结构文学研究助手 C语言代码实现(带源码+解析)... 2019-04-21
java gc的几种方式_GC 的三种基本实现方式 2019-04-21
wget linux java 32_通过wget在Linux上下载Java JDK会显示在许可证页面上 2019-04-21
babylonjs 设置面板位置_babylonjs 空间坐标转为屏幕坐标 2019-04-21
oracle里面如何查询sqlid,CSS_oracle中如何查看sql, --查询表状态:  select uo.O - phpStudy... 2019-04-21
oracle 查询中用case,oracle case when 在查询时候的用法。 2019-04-21
oracle正在运行的程序包,ORACLE PL/SQL编程详解之程序包的创建与应用 2019-04-21
php局部页面滚动,在访问另一页面后保留浏览器滚动位置 - php 2019-04-21
jmeter运行linux命令行,Jmeter在linux上运行(命令行运行Jmeter) 2019-04-21
linux服务器怎么添加站点,如何增加站点或虚拟主机及文件说明 2019-04-21
linux系统输入指令,Linux系统基础 - 基本操作命令 2019-04-21
linux设备管理命令,Linux命令(设备管理).doc 2019-04-21
linux 中文utf-8转gbk编码,Linux平台下 GBK编码转UTF-8编码 2019-04-21
linux安装软件在boot,在Linux系统上安装Spring boot应用的教程详解 2019-04-21