使用IOS7计算文档和数据存储

时间:2014-01-15 11:51:20

标签: ios

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

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

2 个答案:

答案 0 :(得分:1)

首先,您应该注意文件类型。您应该使用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);

答案 1 :(得分:0)

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; }
相关问题