美文网首页
iOS 快速计算目录的size

iOS 快速计算目录的size

作者: NewLand | 来源:发表于2016-07-13 14:39 被阅读25次

    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 // 老谭笔记 

    相关文章

      网友评论

          本文标题: iOS 快速计算目录的size

          本文链接:https://www.haomeiwen.com/subject/msnsjttx.html