容易的方式来获取文件夹大小(ObjC / Cocoa)? [英] Easy way to get size of folder (ObjC/Cocoa)?

查看:237
本文介绍了容易的方式来获取文件夹大小(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,所以你得到资源fork的大小,太!)

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆