美文网首页
计算缓存文件和文件夹大小

计算缓存文件和文件夹大小

作者: 李华光 | 来源:发表于2017-01-12 15:33 被阅读0次

单个文件大小

+ (long long)fileSizeAtPath:(NSString*) filePath {
        NSFileManager* manager = [NSFileManager defaultManager];
        if ([manager fileExistsAtPath:filePath]){
            return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
        }
        return 0;
    }

遍历文件夹获得文件夹大小,返回多少M

+ (float)folderSizeAtPath:(NSString *)folderPath {
        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){
            NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
            folderSize += [self fileSizeAtPath:fileAbsolutePath];
        }
        return folderSize/(1024.0*1024.0);
    }

相关文章

网友评论

      本文标题:计算缓存文件和文件夹大小

      本文链接:https://www.haomeiwen.com/subject/lgndbttx.html