(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

你可能感兴趣的:((iPhone/iPad)计算缓存文件大小)