// // DiskSpaceTool.h // ScreenRecordMaster // // Created by feimo on 15/7/24. // Copyright (c) 2015年 feimo. All rights reserved. // #import <Foundation/Foundation.h> #include <sys/param.h> #include <sys/mount.h> @interface DiskSpaceTool : NSObject + (NSString *) freeDiskSpaceInBytes;//手机剩余空间 + (NSString *) totalDiskSpaceInBytes;//手机总空间 + (NSString *) folderSizeAtPath:(NSString*) folderPath;//某个文件夹占用空间的大小 @end
// // DiskSpaceTool.m // ScreenRecordMaster // // Created by feimo on 15/7/24. // Copyright (c) 2015年 feimo. All rights reserved. // #import "DiskSpaceTool.h" @implementation DiskSpaceTool //手机剩余空间 + (NSString *) freeDiskSpaceInBytes{ struct statfs buf; long long freespace = -1; if(statfs("/var", &buf) >= 0){ freespace = (long long)(buf.f_bsize * buf.f_bfree); } return [self humanReadableStringFromBytes:freespace]; } //手机总空间 + (NSString *) totalDiskSpaceInBytes { struct statfs buf; long long freespace = 0; if (statfs("/", &buf) >= 0) { freespace = (long long)buf.f_bsize * buf.f_blocks; } if (statfs("/private/var", &buf) >= 0) { freespace += (long long)buf.f_bsize * buf.f_blocks; } printf("%lld\n",freespace); return [self humanReadableStringFromBytes:freespace]; } //遍历文件夹获得文件夹大小 + (NSString *) 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 [self humanReadableStringFromBytes:folderSize]; } //单个文件的大小 + (long long) fileSizeAtPath:(NSString*) filePath{ NSFileManager* manager = [NSFileManager defaultManager]; if ([manager fileExistsAtPath:filePath]){ return [[manager attributesOfItemAtPath:filePath error:nil] fileSize]; } return 0; } //计算文件大小 + (NSString *)humanReadableStringFromBytes:(unsigned long long)byteCount { float numberOfBytes = byteCount; int multiplyFactor = 0; NSArray *tokens = [NSArray arrayWithObjects:@"bytes",@"KB",@"MB",@"GB",@"TB",@"PB",@"EB",@"ZB",@"YB",nil]; while (numberOfBytes > 1024) { numberOfBytes /= 1024; multiplyFactor++; } return [NSString stringWithFormat:@"%4.2f %@",numberOfBytes, [tokens objectAtIndex:multiplyFactor]]; } @end
版权声明:本文为博主原创文章,未经博主允许不得转载。
iOS获取文件夹中存储空间的大小、手机剩余空间大小、手机总空间大小 工具
原文地址:http://blog.csdn.net/etmanwenhan/article/details/47046285