// 获取Documents详情 包括子目录以及子目录的size
+ (NSString *)getDocumentsAttrs {
// 获取属性字典
NSString *docPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSDictionary *dic = [self readAttributeOfFileAtPath:docPath];
// 字典转json存入文件
NSData *data = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
NSString *docAttrDetailPath = [docPath stringByAppendingPathComponent:@"DocumentsAttribute.log"];
NSFileManager *fm = [NSFileManager defaultManager];
if (![fm fileExistsAtPath:docAttrDetailPath]) {
[fm createFileAtPath:docAttrDetailPath contents:nil attributes:nil];
}
NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:docAttrDetailPath];
// 清空文件内容
[handle truncateFileAtOffset:0];
[handle writeData:data];
[handle closeFile];
return docAttrDetailPath;
}
static NSString *currentDirName = @"dirName";
static NSString *currentDirSize = @"dirSize";
static NSString *currentDirFormatSize = @"dirFormatSize";
static NSString *currentDirSizeAttrsValue = @"value";
static NSString *isDirectoryKey = @"isDirectory";
static NSString *fileSizeKey = @"fileSize";
static NSString *fileFormatSizeKey = @"fileFormatSize";
// 读取指定路径目录下的文件大小
+ (NSDictionary *)readAttributeOfFileAtPath:(NSString *)pathStr {
if (pathStr.length == 0) {
return @{};
}
NSFileManager *fileManager = [NSFileManager defaultManager];
NSMutableDictionary *tempDic = [NSMutableDictionary dictionaryWithCapacity:1];
if ([fileManager fileExistsAtPath:pathStr]) {
NSError *error = nil;
NSArray *contents = [fileManager contentsOfDirectoryAtPath:pathStr error:&error];
// 因为每个目录(不管是总目录还是子目录)都会走一次for循环,所以和目录相关的变量在for循环外部定义比如 valueDic、 directorySize
NSMutableDictionary *valueDic = [NSMutableDictionary dictionaryWithCapacity:1];
long long directorySize = 0;
for (NSString *str in contents) {
NSLog(@"%@", str);
BOOL isDirectory = NO;
NSString *subPathStr = [NSString stringWithFormat:@"%@/%@",pathStr,str];
[fileManager fileExistsAtPath:subPathStr isDirectory:&isDirectory];
if (isDirectory) {
// 递归
NSDictionary *dirDic = [self readAttributeOfFileAtPath:subPathStr];
if (dirDic[currentDirSize]) {
// 当目录的子目录都是文件类型
directorySize += [dirDic[currentDirSize] longLongValue];
}
else {
// 当目录的子目录中不全是文件类型还有目录类型
directorySize += [dirDic[currentDirSizeAttrsValue][currentDirSize] longLongValue];
}
[tempDic setValue:dirDic forKey:str];
}
else {
NSDictionary *dic = [fileManager attributesOfItemAtPath:subPathStr error:nil];
// NSLog(@"%@", dic);
NSNumber *fileSizeNumber = dic[NSFileSize];
directorySize += fileSizeNumber.longLongValue;
NSDictionary *fileDic = [NSDictionary dictionaryWithObjectsAndKeys:@(0),isDirectoryKey,fileSizeNumber,fileSizeKey,[self formatGivenSize:fileSizeNumber.longLongValue],fileFormatSizeKey, nil];
[tempDic setValue:fileDic forKey:str];
}
}
// 在value这个可以对应的值中放入目录名字
[valueDic setValue:pathStr.lastPathComponent forKey:currentDirName];
[valueDic setValue:@(1) forKey:isDirectoryKey];
[valueDic setValue:@(directorySize) forKey:currentDirSize];
[valueDic setValue:[self formatGivenSize:directorySize] forKey:currentDirFormatSize];
[tempDic setValue:valueDic forKey:currentDirSizeAttrsValue];
}
return tempDic;
}
+ (NSString *)formatGivenSize:(long long)dirSize {
if (dirSize == 0) {
return @"0B";
}
if (dirSize < 1024) {
return [NSString stringWithFormat:@"%lldB",dirSize];
}
if (dirSize < 1024 * 1024) {
return [NSString stringWithFormat:@"%.2fKB",dirSize/1024.0];
}
if (dirSize < 1024 * 1024 * 1024) {
return [NSString stringWithFormat:@"%.2fMB",dirSize/(1024.0 * 1024)];
}
else {
return @"大于等于1G";
}
}