码迷,mamicode.com
首页 > 移动开发 > 详细

iOS获取文件夹中存储空间的大小、手机剩余空间大小、手机总空间大小 工具

时间:2015-07-24 20:45:57      阅读:284      评论:0      收藏:0      [点我收藏+]

标签:ios   存储   

//
//  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获取文件夹中存储空间的大小、手机剩余空间大小、手机总空间大小 工具

标签:ios   存储   

原文地址:http://blog.csdn.net/etmanwenhan/article/details/47046285

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!