-(float)folderSizeAtPath:(NSString *)filePath
{
NSFileManager *manager=[NSFileManager defaultManager];
//判断文件夹是不是存在,不存在的话直接返回0
if (![manager fileExistsAtPath:filePath]) {
return 0;
}
NSArray *array=[manager subpathsAtPath:filePath];//获取指定目录下的所有子目录
NSEnumerator *childFile=[array objectEnumerator];//将子目录转换成枚举对象
long long size=0;
NSString *subpath;
//一次取枚举中的下一个元素,进行非空判断
while ((subpath=[childFile nextObject]) != nil) {
//将指定路径经与其子目录拼接成新的字符串作为判断路径
NSString *newPath=[filePath stringByAppendingPathComponent:subpath];
size+=[self fileSizeAtPath:newPath];//计算大小
}
return size/(1000.0 * 1000.0);//整合后单位为MB(电脑按1000手机按1024)
}
-(long long)fileSizeAtPath:(NSString *)path
{
NSFileManager *fileManager=[NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:path]) {
//返回指定路径下文件的大小
return [[fileManager attributesOfItemAtPath:path error:nil] fileSize];
}
return 0;
}
-(BOOL)removeSameFile:(NSString *)path andCompare:(NSString *)comPath
{
NSFileManager *manager=[NSFileManager defaultManager];
BOOL isSame=[manager contentsEqualAtPath:path andPath:comPath];
if (isSame) {
BOOL isRemove=[manager removeItemAtPath:comPath error:nil];
return isRemove;
}
return NO;
}