获取文件夹大小的简单方法(ObjC/Cocoa)? [英] Easy way to get size of folder (ObjC/Cocoa)?
问题描述
现在我正在使用此代码来获取文件夹的大小:
Right now I'm using this code to get the size of a folder:
NSArray *contents;
NSEnumerator *enumerator;
NSString *path;
contents = [[NSFileManager defaultManager] subpathsAtPath:folderPath];
enumerator = [contents objectEnumerator];
while (path = [enumerator nextObject]) {
NSDictionary *fattrib = [[NSFileManager defaultManager] fileAttributesAtPath:[folderPath stringByAppendingPathComponent:path] traverseLink:YES];
fileSize +=[fattrib fileSize];
}
[contents release];
[path release];
问题在于它非常不准确.它要么增加几兆字节,要么从实际大小中减去几兆字节.例如,我得到了一个 .app 包的文件大小,这个方法报告了 16.2MB,而实际是 15.8.
The problem is that its highly innacurate. It either adds a few megabytes or deducts a few megabytes from the actual size. For example I got the file size of an .app bundle and this method reported 16.2MB, whereas the actual thing is 15.8.
获取文件夹大小的最佳方法是什么?
What's the best way to get the size of a folder?
谢谢
推荐答案
我今天需要自己做这个,我发现Cocoa-dev 列表上的这篇文章 非常快而且匹配 Finder 对字节所说的内容.(不要忘记在 kFSCatInfoRsrcSizes 标志中使用 OR,这样您也可以获得资源分叉大小!)
I needed to do this today myself, and I've found that the code in this post on the Cocoa-dev list is super fast and matches what Finder says to the byte. (don't forget to OR in the kFSCatInfoRsrcSizes flag so you get resource fork sizes, too!)
如果你需要更多关于如何使用它的解释,只需发表评论,我会编辑这篇文章.=)
If you need more explanation on how to use it, just leave a comment and I'll edit this post. =)
这篇关于获取文件夹大小的简单方法(ObjC/Cocoa)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!