ios 获取文件大小方法

//遍历文件夹获得文件夹大小,返回多少M

+ (float ) folderSizeAtPath:(NSString*) folderPath{

    NSFileManager* manager = [NSFileManagerdefaultManager];

    if (![manager fileExistsAtPath:folderPath]) return 0;

    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];

    NSString* fileName;

    long long folderSize = 0;

    while ((fileName = [childFilesEnumerator nextObject]) != nil){

        NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];

        folderSize += [self fileSizeAtPath:fileAbsolutePath];

    }

    return folderSize/(1024.0*1024.0);

}

//单个文件的大小

+ (float) fileSizeAtPath:(NSString*) filePath{

    

    //

    //    NSData* data = [NSData dataWithContentsOfFile:[VoiceRecorderBaseVC getPathByFileName:_convertAmr ofType:@"amr"]];

    //    NSLog(@"amrlength = %d",data.length);

    //    NSString * amr = [NSString stringWithFormat:@"amrlength = %d",data.length];

    

    NSFileManager* manager = [NSFileManagerdefaultManager];

    

    if ([manager fileExistsAtPath:filePath]){

        

        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize]/(1024.0*1024);

    }

    return 0;

 

}

你可能感兴趣的:(文件大小)