• 获取iOS文件夹大小(空间占用)


    + (float)calculateFileSizeInUnit:(unsigned long long)contentLength
    {
        if(contentLength >= pow(1024, 3))
            return (float) (contentLength / (float)pow(1024, 3));
        else if(contentLength >= pow(1024, 2))
            return (float) (contentLength / (float)pow(1024, 2));
        else if(contentLength >= 1024)
            return (float) (contentLength / (float)1024);
        else
            return (float) (contentLength);
    }
    + (NSString *)calculateUnit:(unsigned long long)contentLength
    {
        if(contentLength >= pow(1024, 3))
            return @"GB";
        else if(contentLength >= pow(1024, 2))
            return @"MB";
        else if(contentLength >= 1024)
            return @"KB";
        else
            return @"Bytes";
    }
    + (float ) folderSizeAtPath:(NSString*) folderPath{
        NSFileManager* manager = [NSFileManager defaultManager];
        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;
    }
    + (long long) fileSizeAtPath:(NSString*) filePath{
        NSFileManager* manager = [NSFileManager defaultManager];
        if ([manager fileExistsAtPath:filePath]){
            return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
        }
        return 0;
    }
    
    +(NSString *)folderSizeStringAtPath:(NSString*) filePath
    {
        float size=[self folderSizeAtPath:filePath];
        NSString *fileSizeString=[NSString stringWithFormat:@"%.02f%@",[self calculateFileSizeInUnit:size],[self calculateUnit:size]];
        return fileSizeString;
    }
  • 相关阅读:
    滴水穿石mysql添加授权用户命令
    滴水穿石修改IIS服务器的MIME类型
    滴水穿石Java静态代理
    滴水穿石MyEclipse中集成开发插件Spket
    滴水穿石Java连接Access数据库及其操作
    好站收藏
    apache缓存设置
    Linux Apache开启gzip压缩提高网页下载速度
    web制作、开发人员需知的Web缓存知识
    cookiec和session学习笔记
  • 原文地址:https://www.cnblogs.com/ashamp/p/4067418.html
Copyright © 2020-2023  润新知