title: How to calculate the size of a folder. (This size must agree with Finder.)
body: <p>I need to calculate the size of a folder in MacOS. This size value must agree with Finder. I've tried several ways to do this. but the results are always different from Finder. </p>
  
  <p>the following methods are what I tried. </p>
  
  <pre><code>typedef struct{
      //getResourceValue in NSURL
      unsigned long long fileSize;
      unsigned long long fileAllocSize;
      unsigned long long totalFileSize;
      unsigned long long totalFileAllocSize;
      //lstat in posix
      unsigned long long statSize;
      unsigned long long blockSize;
      //NSFileManager
      unsigned long long cocoaSizeNo;
      unsigned long long cocoaSizeYes
  }sizePack;
  
  - (sizePack)foldSize:(NSString *)direString{
  
      sizePack sizeP;
      memset(&amp;sizeP, 0, sizeof(sizeP));
  
      NSFileManager *fileManager = [NSFileManager defaultManager];
  
      NSArray *tempArray = [fileManager contentsOfDirectoryAtPath:direString error:nil];
      for (NSString *fileName in tempArray) {
          BOOL flag = YES;
          NSString *fullPath = [direString stringByAppendingPathComponent:fileName];
          if ([fileManager fileExistsAtPath:fullPath isDirectory:&amp;flag]) {
              if (!flag) {
  
                  NSNumber* FileSize;
                  NSNumber* FileAllocatedSize;
                  NSNumber* TotalFileSize;
                  NSNumber* TotalAllocatedSize;
  
                  NSURL* url = [NSURL fileURLWithPath:fullPath];
                  [url getResourceValue:&amp;FileSize forKey:NSURLFileSizeKey error:nil];
                  [url getResourceValue:&amp;FileAllocatedSize forKey:NSURLFileAllocatedSizeKey error:nil];
                  [url getResourceValue:&amp;TotalFileSize forKey:NSURLTotalFileSizeKey error:nil];
                  [url getResourceValue:&amp;TotalAllocatedSize forKey:NSURLTotalFileAllocatedSizeKey error:nil];
  
  
                  struct stat statbuf;
                  stat([fullPath UTF8String], &amp;statbuf);
  
                  unsigned long long fileSize = [FileSize unsignedLongLongValue];
                  unsigned long long fileAllocSize = [FileAllocatedSize unsignedLongLongValue];
                  unsigned long long totalFileSize = [TotalFileSize unsignedLongLongValue];
                  unsigned long long totalFileAllocSize = [TotalAllocatedSize unsignedLongLongValue];
                  unsigned long long cocoaSizeNO = [[[NSFileManager defaultManager] fileAttributesAtPath:fullPath traverseLink:NO] fileSize];
  
                  unsigned long long cocoaSizeYES = [[[NSFileManager defaultManager] fileAttributesAtPath:fullPath traverseLink:YES] fileSize];
  
  
                  sizeP.fileSize += fileSize;
                  sizeP.fileAllocSize += fileAllocSize;
                  sizeP.totalFileSize += totalFileSize;
                  sizeP.totalFileAllocSize += totalFileAllocSize;
                  sizeP.statSize += statbuf.st_size;
                  sizeP.blockSize += statbuf.st_blksize;
                  sizeP.cocoaSizeNo += cocoaSizeNO;
                  sizeP.cocoaSizeYes += cocoaSizeYES;
              }
              else {
                  sizePack tmp = [self foldSize:fullPath];
                  sizeP.fileSize += tmp.fileSize;
                  sizeP.fileAllocSize += tmp.fileAllocSize;
                  sizeP.totalFileSize += tmp.totalFileSize;
                  sizeP.totalFileAllocSize += tmp.totalFileAllocSize;
                  sizeP.statSize += tmp.statSize;
                  sizeP.blockSize += tmp.blockSize*4096;
                  sizeP.cocoaSizeNo += tmp.cocoaSizeNo;
                  sizeP.cocoaSizeYes += tmp.cocoaSizeYes;
              }
          }
      }
  
      return sizeP;
  }
  </code></pre>