通过以下两个方法获取文件夹大小:
//获取文件大小
- (long long) fileSizeAtPath:(NSString*) filePath{
NSFileManager* manager = [NSFileManager defaultManager];
if ([manager fileExistsAtPath:filePath]){
return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
}
return 0;
}
//获取内容大小
- (CGFloat)getCacheSize
{
home=[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
// home = [home stringByAppendingPathComponent:@"temp"];
// [home retain];
NSFileManager* manager = [NSFileManager defaultManager];
if (![manager fileExistsAtPath:home]) return 0;
NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:home] objectEnumerator];
NSString* fileName;
long long folderSize = 0;
while ((fileName = [childFilesEnumerator nextObject]) != nil){
NSString* fileAbsolutePath = [home stringByAppendingPathComponent:fileName];
folderSize += [self fileSizeAtPath:fileAbsolutePath];
}
return folderSize/(1024.0*1024.0);
}