首页 > 其他 > 详细

计算文件大小的分类

时间:2016-05-07 12:47:06      阅读:84      评论:0      收藏:0      [点我收藏+]
@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

 

计算文件大小的分类

原文:http://www.cnblogs.com/yintingting/p/5467755.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!