iOS初学者

NSFileManager 获取某个路径下的文件大小

2022-04-26  本文已影响0人  上北以北
//获取某个路径下所有文件大小
- (long long)fileSizeForPath:(NSString*)path {
    long long size = 0;
    
    if (!path.length) {
        return size;
    }
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray<NSString *>* subPaths = [fileManager contentsOfDirectoryAtPath:path error:nil];
    
    if (subPaths.count) {
        for (NSString *childPath in subPaths) {
            NSString *c_path = [path stringByAppendingPathComponent:childPath];
            if ([fileManager fileExistsAtPath:c_path]) {
                size += [self fileSizeForPath:c_path];
            }
        }
    } else {
        NSDictionary *fileAttributeDic = [fileManager attributesOfItemAtPath:path error:nil];
        size = fileAttributeDic.fileSize;
    }
        
    return size;
}
上一篇下一篇

猜你喜欢

热点阅读