美文网首页iOS开发
iOS 文件和文件夹的计算

iOS 文件和文件夹的计算

作者: Kimball | 来源:发表于2015-12-21 10:40 被阅读67次

    -(float)fileSizeAtPath:(NSString *)filePath

    {

    NSFileManager *manager=[ NSFileManager defaultManager];

    if ([manager fileExistsAtPath:filePath]) {

    NSDictionary *dict= [manager attributesOfItemAtPath:filePath

    error:nil];

    return [dict fileSize ]/1024.0/1024.0;

    } else {

    return 0.0;

    }

    }

    -(float)folderSizeAtPath:(NSString *)folderPath

    {

    NSFileManager *manager=[NSFileManager defaultManager];

    if ([manager fileExistsAtPath:folderPath]) {

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

    NSString *fileName;

    float folderSize = 0;

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

    NSString *fileAbsolutePath=[folderPath stringByAppendingPathComponent:fileName];

    folderSize+=[self fileSizeAtPath:fileAbsolutePath];

    }

    return folderSize;

    } else {

    return 0.0;

    }

    }

    相关文章

      网友评论

        本文标题:iOS 文件和文件夹的计算

        本文链接:https://www.haomeiwen.com/subject/rkemhttx.html