计算文件夹下文件的总大小

来源:互联网 发布:怎么在淘宝上发布虚拟 编辑:程序博客网 时间:2024/05/16 12:04

计算文件夹下文件的总大小


+(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;

}

0 0