關(guān)于目錄的size計(jì)算,我在網(wǎng)上查了很多種方式去實(shí)現(xiàn)過(guò),最常見(jiàn)的就是通過(guò)遞歸方式去逐層計(jì)算,但通過(guò)測(cè)試發(fā)現(xiàn)在計(jì)算層數(shù)多、數(shù)量大的目錄時(shí),遞歸的方式會(huì)消耗很大的??臻g,甚至出現(xiàn)棧溢出,當(dāng)然效率也會(huì)是很大的瓶頸。最初通過(guò)將NSFileManager獲取單個(gè)文件size修改為FS的函數(shù)提升效率,到后來(lái)看到老譚 改用效率更高的stat方式,并且不僅可能獲得文件實(shí)際大小,也能獲得占用磁盤(pán)的大小。另外通過(guò)實(shí)現(xiàn)棧的方式去替換掉遞歸,不僅節(jié)省了內(nèi)存占用,讓效率也有大幅的提升。
下面就是具體的代碼實(shí)現(xiàn),通過(guò)NSMutableArray來(lái)模擬棧(經(jīng)測(cè)試用數(shù)組頭作為棧頂相比數(shù)組尾部做棧頂效率更高,這與NSMutableArray內(nèi)部實(shí)現(xiàn)有關(guān),也嘗試不依賴NSMutableArray而去實(shí)現(xiàn)棧但效率提升有限),并通過(guò)diskMode參數(shù)可返回是否是磁盤(pán)占用的size:
+ (uint64_t)sizeAtPath:(NSString*)filePath diskMode:(BOOL)diskMode
{
uint64_ttotalSize =0;
NSMutableArray*searchPaths = [NSMutableArrayarrayWithArray:NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask,YES)] ;
[searchPathsaddObjectsFromArray:NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)];
[searchPathsaddObject:NSTemporaryDirectory()];
while([searchPathscount] >0)
{
@autoreleasepool
{
NSString*fullPath = [searchPathsobjectAtIndex:0];
[searchPathsremoveObjectAtIndex:0];
structstatfileStat;
if(lstat([fullPathfileSystemRepresentation], &fileStat) ==0)
{
if(fileStat.st_mode&S_IFDIR)
{
NSArray*childSubPaths = [[NSFileManagerdefaultManager]contentsOfDirectoryAtPath:fullPatherror:nil];
for(NSString*childIteminchildSubPaths)
{
NSString*childPath = [fullPathstringByAppendingPathComponent:childItem];
[searchPathsinsertObject:childPathatIndex:0];
}
}else
{
if(diskMode)
totalSize += fileStat.st_blocks*512;
else
totalSize += fileStat.st_size;
}
}
}
}
returntotalSize;
}