如何计算文件夹的大小? [英] How can I calculate the size of a folder?

查看:226
本文介绍了如何计算文件夹的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个文件夹,用我的iPhone App缓存Documents中的图像。我希望能够将此文件夹的大小保持在1MB,因此我需要检查文件夹的大小(以字节为单位)。

I'm creating a folder to cache images inside Documents with my iPhone App. I want to be able to keep the size of this folder down to 1MB, so I need to to check the size in bytes of my folder.

我有要计算的代码文件大小,但我需要文件夹的大小。

I have code to calculate the size of file, but I need the size of the folder.

最好的方法是什么?

推荐答案

tl; dr



所有其他答案都已关闭:)

tl;dr

All the other answers are off :)

我想在这个老问题上加上我的两分钱,因为似乎有很多答案都非常相似但产生的结果在某些情况下非常不合理。

I'd like to add my two cents to this old question as there seem to be many answers that are all very similar but yield results that are in some cases very unprecise.

要理解为什么我们首先必须定义文件夹的大小。根据我的理解(可能是OP中的一个),它是包含其所有内容的目录在卷上使用的字节数。或者,换句话说:

To understand why we first have to define what the size of a folder is. In my understanding (and probably the one of the OP) it is the amount of bytes that the directory including all of its contents uses on the volume. Or, put in another way:

如果目录被完全删除,那么空间就变得可用。

我知道这个定义并不是解释问题的唯一有效方法,但我认为这是大多数用例归结为的。

I'm aware that this definition is not the only valid way to interpret the question but I do think it's what most use cases boil down to.

现有答案都采用一种非常简单的方法:遍历目录内容,累加(常规)文件的大小。这不需要考虑一些细微之处。

The existing answers all take a very simple approach: Traverse the directory contents, adding up the sizes of (regular) files. This does not take a couple of subtleties into account.


  • 卷上使用的空间在中递增,不是字节。即使是一个单字节文件也至少使用一个块。

  • 文件带有元数据(与任意数量的扩展属性一样)。这些数据必须在某个地方。

  • HFS部署文件系统压缩以使用更少的字节然后实际存储文件。

  • The space used on the volume increments in blocks, not in bytes. Even a one byte file uses at least one block.
  • Files carry around meta data (like any number of extended attributes). This data must go somewhere.
  • HFS deploys file system compression to actually store the file using less bytes then its real length.

所有这些原因使得现有答案产生了不恰当的结果。所以我在 NSFileManager 上建议这个扩展(github上的代码由于长度: Swift 4 目标C )解决问题。它也快得多,特别是对于包含大量文件的目录。

All of these reasons make the existing answers produce unprecise results. So I'm proposing this extension on NSFileManager (code on github due to length: Swift 4, Objective C) to remedy the problem. It's also quite a bit faster, especially with directories containing a lot of files.

解决方案的核心是使用 NSURL NSURLTotalFileAllocatedSizeKey NSURLFileAllocatedSizeKey 检索文件大小的权限。

The core of the solution is to use NSURL's NSURLTotalFileAllocatedSizeKey or NSURLFileAllocatedSizeKey properies to retrieve file sizes.

我还设置了一个简单的iOS测试项目,展示了解决方案之间的差异。它显示了在某些情况下结果完全错误。

I've also set up a simple iOS test project, demonstrating the differences between the solutions. It shows how utterly wrong the results can be in some scenarios.

在测试中,我创建了一个包含100个小文件(范围从0到800字节)的目录。从其他答案复制的 folderSize:方法计算总计21 kB,而我的 allocatedSize 方法产生401 kB。

In the test I create a directory containing 100 small files (ranging from 0 to 800 bytes). The folderSize: method copied from some other answer calculates a total of 21 kB while my allocatedSize method yields 401 kB.

我确保 allocatedSize 通过计算删除测试目录之前和之后卷上可用字节的差异,使其更接近正确的值。在我的测试中,差异总是完全等于 allocatedSize 的结果。

I made sure that the results of allocatedSize are closer to the correct value by calculating the difference of the available bytes on the volume before and after deleting the test directory. In my tests the difference was always exactly equal to the result of allocatedSize.

请参阅Rob Napier的评论以了解还有改进的余地。

Please see Rob Napier's comment to understand that there's still room for improvement.

但还有另一个好处:计算目录大小时1000个文件,在我的iPhone 6上, folderSize:方法大约需要250毫秒,而 allocatedSize 遍历35个相同的层次结构ms。

But there's another advantage: When calculating the size of a directory with 1000 files, on my iPhone 6 the folderSize: method takes about 250 ms while allocatedSize traverses the same hierarchy in 35 ms.

这可能是由于使用 NSFileManager 的新(ish) enumeratorAtURL :includesPropertiesForKeys:options:errorHandler:用于遍历层次结构的API。此方法允许您为要迭代的项目指定预取属性,从而减少io。

This is probably due to using NSFileManager's new(ish) enumeratorAtURL:includingPropertiesForKeys:options:errorHandler: API to traverse the hierachy. This method let's you specify prefetched properties for the items to be iterated, resulting in less io.

Test `folderSize` (100 test files)
    size: 21 KB (21.368 bytes)
    time: 0.055 s
    actual bytes: 401 KB (401.408 bytes)

Test `allocatedSize` (100 test files)
    size: 401 KB (401.408 bytes)
    time: 0.048 s
    actual bytes: 401 KB (401.408 bytes)

Test `folderSize` (1000 test files)
    size: 2 MB (2.013.068 bytes)
    time: 0.263 s
    actual bytes: 4,1 MB (4.087.808 bytes)

Test `allocatedSize` (1000 test files)
    size: 4,1 MB (4.087.808 bytes)
    time: 0.034 s
    actual bytes: 4,1 MB (4.087.808 bytes)

这篇关于如何计算文件夹的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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