ios - 计算文档和数据存储使用 ios

  显示原文与译文双语对照的内容

我正在尝试查找我的IOS应用程序的文档目录的总大小。 我相信这是我在使用( 文档和数据,此值现在为 40 MB )的设置应用程序中看到的值。 通过使用这里方法:


-(int)sizeOfFolder:(NSString *)folderPath
{
NSArray *contents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:folderPath error:nil];
NSEnumerator *contentsEnumurator = [contents objectEnumerator];

NSString *file;
unsigned long long int folderSize = 0;

while (file = [contentsEnumurator nextObject]) {
 NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[folderPath stringByAppendingPathComponent:file] error:nil];
 folderSize += [[fileAttributes objectForKey:NSFileSize] intValue];
}

//This line will give you formatted size from bytes.. ..
//NSString *folderSizeStr = [NSByteCountFormatter stringFromByteCount:folderSize countStyle:NSByteCountFormatterCountStyleFile];

NSLog(@"size: %llu", folderSize);

return folderSize;
} 

我总是有同样的价值。 这是我传递给方法的路径:


NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* rootPath = [paths objectAtIndex:0];
int space = [self sizeOfFolder:rootPath];

时间: 作者:

首先,你应该小心使用文件类型。 应该使用 long long 来计算总大小:


unsigned long long folderSize = 0;//remove int here
//...
folderSize += [[fileAttributes objectForKey:NSFileSize] longLongValue];//note longLongValue

接下来,你应该跳过目录以获得更精确的结果


if (![fileAttirbutes[NSFileType] equalsToString:NSFileTypeDirectory]) {
 folderSize +=.. .
}

我建议你查看另一个目录枚举方法。 使用 - (NSDirectoryEnumerator *)enumeratorAtURL:(NSURL *)url includingPropertiesForKeys:(NSArray *)keys options:(NSDirectoryEnumerationOptions)mask errorHandler:(BOOL (^)(NSURL *url, NSError *error))handler 将允许你执行更深入的目录枚举并递归列出所有目录内容


unsigned long long count = 0;
NSNumber *value = nil;

for (NSURL *url in [[NSFileManager defaultManager] enumeratorAtURL:directoryURL includingPropertiesForKeys:@[NSURLFileSizeKey] options:0 errorHandler:NULL]) {
 if ([url getResourceValue:&value forKey:NSURLFileSizeKey error:outError]) {
 count += [value longLongValue];
 } else {
 return nil;
 }
}

return @(count);

作者:

NSFileManager没有返回文件夹( 文件夹+ 内容)的大小的能力。 在网上发现这个。


- (unsigned long long) fastFolderSizeAtFSRef:(FSRef*)theFileRef {
FSIterator thisDirEnum = NULL;
unsigned long long totalSize = 0;

//Iterate the directory contents, recursing as necessary
if (FSOpenIterator(theFileRef, kFSIterateFlat, &thisDirEnum) == noErr)
{
 const ItemCount kMaxEntriesPerFetch = 256;
 ItemCount actualFetched;
 FSRef fetchedRefs[kMaxEntriesPerFetch];
 FSCatalogInfo fetchedInfos[kMaxEntriesPerFetch];

//DCJ Note right now this is only fetching data fork sizes... if we decide to include
//resource forks we will have to add kFSCatInfoRsrcSizes

 OSErr fsErr = FSGetCatalogInfoBulk(thisDirEnum, kMaxEntriesPerFetch, &actualFetched,
 NULL, kFSCatInfoDataSizes | kFSCatInfoNodeFlags, fetchedInfos,
 fetchedRefs, NULL, NULL);
 while ((fsErr == noErr) || (fsErr == errFSNoMoreItems))
 {
 ItemCount thisIndex;
 for (thisIndex = 0; thisIndex <actualFetched; thisIndex++)
 {
//Recurse if it's a folder
 if (fetchedInfos[thisIndex].nodeFlags & kFSNodeIsDirectoryMask)
 {
 totalSize += [self fastFolderSizeAtFSRef:&fetchedRefs[thisIndex]];
 }
 else
 {

 totalSize += fetchedInfos [thisIndex].dataLogicalSize;
 }
 }

 if (fsErr == errFSNoMoreItems)
 {
 break;
 }
 else
 {
//get more items
 fsErr = FSGetCatalogInfoBulk(thisDirEnum, kMaxEntriesPerFetch, &actualFetched,
 NULL, kFSCatInfoDataSizes | kFSCatInfoNodeFlags, fetchedInfos,
 fetchedRefs, NULL, NULL);
 }
 }
 FSCloseIterator(thisDirEnum);
}
return totalSize; }

作者:
...