diskMode為YES時(shí)是指占用磁盤的空間(磁盤占用空間與文件的實(shí)際大小一般是不一樣的)
+ (uint64_t)sizeAtPath:(NSString*)filePath diskMode:(BOOL)diskMode
{
uint64_t totalSize =0;
NSMutableArray*searchPaths = [NSMutableArrayarrayWithObject:filePath];
while([searchPaths count] >0)
{
@autoreleasepool
{
NSString*fullPath = [searchPaths objectAtIndex:0];
[searchPaths removeObjectAtIndex:0];
structstat fileStat;
if(lstat([fullPath fileSystemRepresentation], &fileStat) ==0)
{
if(fileStat.st_mode& S_IFDIR)
{
NSArray*childSubPaths = [[NSFileManagerdefaultManager] contentsOfDirectoryAtPath:fullPath error:nil];
for(NSString*childIteminchildSubPaths)
{
NSString*childPath = [fullPath stringByAppendingPathComponent:childItem];
[searchPaths insertObject:childPath atIndex:0];
}
}else
{
if(diskMode)
totalSize += fileStat.st_blocks*512;
else
totalSize += fileStat.st_size;
}
}
}
}
returntotalSize;
}
轉(zhuǎn)載:快速計(jì)算目錄的size // 老譚筆記?