您的位置:首页 > 其它

OC中根据文件路径获取文件大小

2016-11-03 15:10 225 查看
NSString * cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,
NSUserDomainMask,
YES) firstObject];

    

NSInteger  size =  [self
getSizeOfFilePath:cachePath];  // 文件大小

-(NSInteger)getSizeOfFilePath:(NSString *)filePath{

    /** 定义记录大小 */

    NSInteger totalSize =
0;

    /** 创建一个文件管理对象 */

    NSFileManager * manager = [NSFileManager
defaultManager];

    /**获取文件下的所有路径包括子路径 */

    NSArray * subPaths = [manager
subpathsAtPath:filePath];

    /** 遍历获取文件名称 */

    for (NSString * fileName
in subPaths) {

        /** 拼接获取完整路径 */

        NSString * subPath = [filePath
stringByAppendingPathComponent:fileName];

        /** 判断是否是隐藏文件 */

        if ([fileName
hasPrefix:@".DS"]) {

            continue;

        }

        /** 判断是否是文件夹 */

        BOOL isDirectory;

        [manager fileExistsAtPath:subPath
isDirectory:&isDirectory];

        if (isDirectory) {

            continue;

        }

        /** 获取文件属性 */

        NSDictionary *dict = [manager
attributesOfItemAtPath:subPath
error:nil];

        /** 累加 */

        totalSize += [dict fileSize];

        

    }

    /** 返回 */

    return totalSize;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: