易网时代-编程资源站
Welcome
微信登录
编程资源
图片资源库
蚂蚁家优选
PDF转换器
软件资源
软件开发
、
小程序制作
、
系统集成与运维
、
空间租用
、
硬件开发
、
视频监控
、
技术咨询与支持
——联系电话:0311-88999002/88999003
首页
/
操作系统
/
Linux
/
(iPhone/iPad)计算缓存文件大小
(iPhone/iPad)计算缓存文件大小:
FileSize.h
//
// FileSize.h
//
//
#import <Cocoa/Cocoa.h>
@interface FileSize : NSObject {
}
// This method converts a given # of bytes into human readable format (KB, MB, GB)
- (NSString *)stringFromFileSize:(
int
)theSize;
// Returns the size of a file in bytes
- (
int
)sizeOfFile:(NSString *)path;
// Returns the size of a folder in bytes
- (
int
)sizeOfFolder:(NSString *)path;
@end
FileSize.m
// FileSize.m
// Created by PCWiz on 30/07/09.
#import "FileSize.h"
@implementation FileSize
- (NSString *)stringFromFileSize:(
int
)theSize
{
float
floatSize = theSize;
if
(theSize<1023)
return
([NSString stringWithFormat:@
"%i bytes"
,theSize]);
floatSize = floatSize / 1024;
if
(floatSize<1023)
return
([NSString stringWithFormat:@
"%1.1f KB"
,floatSize]);
floatSize = floatSize / 1024;
if
(floatSize<1023)
return
([NSString stringWithFormat:@
"%1.1f MB"
,floatSize]);
floatSize = floatSize / 1024;
return
([NSString stringWithFormat:@
"%1.1f GB"
,floatSize]);
}
- (
int
)sizeOfFile:(NSString *)path
{
NSDictionary *fattrib = [[NSFileManager defaultManager] fileAttributesAtPath:path traverseLink:YES];
int
fileSize = (
int
)[fattrib fileSize];
return
fileSize;
}
- (
int
)sizeOfFolder:(NSString*)folderPath
{
NSArray *contents;
NSEnumerator *enumerator;
NSString *path;
contents = [[NSFileManager defaultManager] subpathsAtPath:folderPath];
enumerator = [contents objectEnumerator];
int
fileSizeInt = 0;
while
(path = [enumerator nextObject]) {
NSDictionary *fattrib = [[NSFileManager defaultManager] fileAttributesAtPath:[folderPath stringByAppendingPathComponent:path] traverseLink:YES];
fileSizeInt +=[fattrib fileSize];
}
return
fileSizeInt;
}
@end
收藏该网址
版权所有©石家庄振强科技有限公司2024
冀ICP备08103738号-5
网站地图