+ (long long)fileSizeAtPath:(NSString*)filePath
{
NSFileManager* manager = [NSFileManager defaultManager];
if ([manager fileExistsAtPath:filePath])
{
return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
}
return 0;
}
#pragma mark 计算目录大小
+ (float)getCacheSizeFromFolder:(NSString *)folderName
{
// 获取Caches目录路径
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask,YES);
NSString *folderPath;
if (folderName == nil) {
folderPath = [paths objectAtIndex:0];
} else {
NSString *cachePath = [paths objectAtIndex:0];
folderPath = [cachePath stringByAppendingPathComponent:folderName];
}
NSFileManager* manager = [NSFileManager defaultManager];
if (![manager fileExistsAtPath:folderPath]) return 0;
NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];//从前向后枚举器
NSString* fileName;
long long folderSize = 0;
while ((fileName = [childFilesEnumerator nextObject]) != nil)
{
NSLog(@"fileName ==== %@",fileName);
NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
NSLog(@"fileAbsolutePath ==== %@",fileAbsolutePath);
folderSize += [self fileSizeAtPath:fileAbsolutePath];
}
NSLog(@"folderSize ==== %lld",folderSize);
return folderSize/(1024.0*1024.0);
}
#pragma mark 清理缓存
+ (void)clearCacheFromFolder:(NSString *)folderName
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask,YES);
NSString *path;
if (folderName == nil) {
path = [paths objectAtIndex:0];
} else {
NSString *cachePath = [paths objectAtIndex:0];
path = [cachePath stringByAppendingPathComponent:folderName];
}
NSFileManager *fileManager=[NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:path]) {
NSArray *childerFiles=[fileManager subpathsAtPath:path];
for (NSString *fileName in childerFiles) {
//如有需要,加入条件,过滤掉不想删除的文件
NSString *absolutePath=[path stringByAppendingPathComponent:fileName];
[fileManager removeItemAtPath:absolutePath error:nil];
}
}
}
iOS 获取文件大小,清除缓存
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 接着上篇下载word/pdf/txt等文档的文章,此篇涉及到清除这些文档。本文实现文件/文件夹大小的计算以及清除文...