iOS 快速计算目录的size

2016-07-13  本文已影响25人  NewLand

diskMode为YES时是指占用磁盘的空间(磁盘占用空间与文件的实际大小一般是不一样的)

+ (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;

}

转载:快速计算目录的size // 老谭笔记 

上一篇下一篇

猜你喜欢

热点阅读