iOS 文件和文件夹的计算

-(float)fileSizeAtPath:(NSString *)filePath

{

NSFileManager *manager=[ NSFileManager defaultManager];

if ([manager fileExistsAtPath:filePath]) {

NSDictionary *dict= [manager attributesOfItemAtPath:filePath

error:nil];

return [dict fileSize ]/1024.0/1024.0;

} else {

return 0.0;

}

}

-(float)folderSizeAtPath:(NSString *)folderPath

{

NSFileManager *manager=[NSFileManager defaultManager];

if ([manager fileExistsAtPath:folderPath]) {

NSEnumerator *childFileEnumertor=[[manager subpathsAtPath:folderPath] objectEnumerator];

NSString *fileName;

float folderSize = 0;

while ((fileName=[childFileEnumertor nextObject])!=nil) {

NSString *fileAbsolutePath=[folderPath stringByAppendingPathComponent:fileName];

folderSize+=[self fileSizeAtPath:fileAbsolutePath];

}

return folderSize;

} else {

return 0.0;

}

}

你可能感兴趣的:(iOS 文件和文件夹的计算)