计算文件大小的分类

@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
原文地址:https://www.cnblogs.com/yintingting/p/5467755.html