// 递归计算某路径下所有文件大小,单位B,除以1024单位为KB,除以1024*1024单位为M
- (void)getCacheSizeWithFilePath:(NSString *)path totalSize:(long long *)totalSize {
// 备注:[SDImageCache sharedImageCache].getSize没有计入
NSFileManager *fm = [NSFileManager defaultManager];
BOOL isDir = NO;
BOOL isExist = [fm fileExistsAtPath:path isDirectory:&isDir];
if (isExist) {
if (isDir) {
NSArray *subPaths = [fm subpathsAtPath:path];
for (NSString *sub in subPaths) {
NSString *absolutePath = [path stringByAppendingPathComponent:sub];
[self getCacheSizeWithFilePath:absolutePath totalSize:&*totalSize];
}
} else {
*totalSize += [fm attributesOfItemAtPath:path error:nil].fileSize;
}
}
}
// 递归清理某路径下所有文件
- (void)clearCacheWithFilePath:(NSString *)path {
// 备注:[[SDImageCache sharedImageCache] clearDisk]没有清理
NSFileManager *fm = [NSFileManager defaultManager];
BOOL isDir = NO;
BOOL isExist = [fm fileExistsAtPath:path isDirectory:&isDir];
if (isExist) {
if (isDir) {
NSArray *subPaths = [fm subpathsAtPath:path];
for (NSString *sub in subPaths) {
NSString *absolutePath = [path stringByAppendingPathComponent:sub];
[fm removeItemAtPath:absolutePath error:nil];
}
} else {
[fm removeItemAtPath:path error:nil];
}
}
}
// 将 mainBundle 保存文件到 NSDocumentDirectory
- (void)saveToDocument:(NSString *)fileName {
NSString *filePath = [[NSBundle mainBundle] pathForAuxiliaryExecutable:fileName];
NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
NSString *dstPath = [path stringByAppendingPathComponent:fileName];
if ([[NSFileManager defaultManager] fileExistsAtPath:dstPath]) {
[[NSFileManager defaultManager] removeItemAtPath:dstPath error:nil];
}
[[NSFileManager defaultManager] copyItemAtPath:filePath toPath:dstPath error:nil];
}
// 递归读取某路径下的所有规定后缀的文件
- (void)getAllFileWithPath:(NSString *)path
suffix:(NSString *)suffix
allFile:(NSMutableArray **)allFile {
NSFileManager *fm = [NSFileManager defaultManager];
BOOL isDir = NO;
BOOL isExist = [fm fileExistsAtPath:path isDirectory:&isDir];
if (isExist) {
if (isDir) {
NSArray *dirArray = [fm contentsOfDirectoryAtPath:path error:nil];
for (NSString *str in dirArray) {
NSString *subPath = [path stringByAppendingPathComponent:str];
[self getAllFileWithPath:subPath suffix:suffix allFile:&*allFile];
}
} else {
if ([path hasSuffix:suffix]) {
[*allFile addObject:path];
}
}
}
}