您的位置:首页 > 其它

计算文件大小的分类

2016-05-07 09:32 225 查看
@implementation NSString (fileSize)

- (unsigned long long)fileSize
{
// 总大小
unsigned long long size = 0;

// 文件管理者
NSFileManager *mgr = [NSFileManager defaultManager];

// 文件属性
NSDictionary *attrs = [mgr attributesOfItemAtPath:self error:nil];

if ([attrs.fileType isEqualToString:NSFileTypeDirectory]) { // 文件夹
// 获得文件夹的大小  == 获得文件夹中所有文件的总大小
NSDirectoryEnumerator *enumerator = [mgr enumeratorAtPath:self];
for (NSString *subpath in enumerator) {
// 全路径
NSString *fullSubpath = [self stringByAppendingPathComponent:subpath];
// 累加文件大小
if ([[mgr attributesOfItemAtPath:fullSubpath error:nil].fileType isEqualToString:NSFileTypeDirectory]) { //如果遍历到的是文件夹,那么继续遍历,只增加文件(而不是文件夹)的大小
continue;
}
size += [mgr attributesOfItemAtPath:fullSubpath error:nil].fileSize;
NSLog(@"---%@",fullSubpath);
}
} else { // 文件
size = attrs.fileSize;

}

return size;
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: