获取文件夹总大小
+ (long long)getTotleFileSize{
NSFileManager* fileManager=[NSFileManager defaultManager];
NSDirectoryEnumerator * enumerator = [fileManager enumeratorAtPath:NSHomeDirectory()];
long long totalSize = 0;
NSString * aPath = @"";
while (aPath = [enumerator nextObject]) {
NSString * realPath = [NSHomeDirectory() stringByAppendingPathComponent:aPath];
NSDictionary * dic = [fileManager attributesOfItemAtPath:realPath error:nil];
totalSize += [dic[@"NSFileSize"] longLongValue];
}
NSLog(@"------- 总大小为: %.2f",totalSize/1000.0/1000.0);
totalSize = totalSize/1000.0/1000.0;
return totalSize;
}
获取某个文件夹大小
+(float)fileSizeForDir:(NSString*)path
{
NSFileManager *fileManager = [[NSFileManager alloc] init];
float size =0;
NSArray* array = [fileManager contentsOfDirectoryAtPath:path error:nil];
for(int i = 0; i<[array count]; i++)
{
NSString *fullPath = [path stringByAppendingPathComponent:[array objectAtIndex:i]];
BOOL isDir;
if ( !([fileManager fileExistsAtPath:fullPath isDirectory:&isDir] && isDir) )
{
NSDictionary *fileAttributeDic=[fileManager attributesOfItemAtPath:fullPath error:nil];
size+= fileAttributeDic.fileSize/ 1024.0/1024.0;
}
else
{
[self fileSizeForDir:fullPath];
}
}
return size;
}
获取某文件Documents下的路径
+ (NSString*)GetPath:(NSString*)fileName
{
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
return fullPathToFile;
}
删除沙盒里对应文件
+ (void)deleteFileWithImageName:(NSString*)str {
NSFileManager* fileManager=[NSFileManager defaultManager];
// NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
// NSString *uniquePath=[[paths objectAtIndex:0] stringByAppendingPathComponent:str];
NSString *uniquePath=[self GetPath:str];
BOOL Have=[[NSFileManager defaultManager] fileExistsAtPath:uniquePath];
if (!Have) {
NSLog(@"no have");
return ;
}else {
NSLog(@" have");
BOOL Delet= [fileManager removeItemAtPath:uniquePath error:nil];
if (Delet) {
NSLog(@"delete success");
}else {
NSLog(@"delete failed");
}
}
}