【问题标题】:calculating directory size in cocoa计算可可中的目录大小
【发布时间】:2017-02-03 20:45:51
【问题描述】:

我想计算目录(文件夹)的大小,我必须列出卷(驱动器)中的所有文件和文件夹(子文件夹)及其相应的大小。我正在使用下面的代码来计算大小。这个问题代码是性能问题。我正在使用NSBrowser 来显示。

NSArray *filesArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:folderPath error:nil];
NSEnumerator *filesEnumerator = [filesArray objectEnumerator];
NSString *fileName;
unsigned long long int fileSize = 0;

while (fileName = [filesEnumerator nextObject]) 
{
    NSDictionary *fileDictionary = [[NSFileManager defaultManager] attributesOfItemAtPath:folderPath error:nil];
    fileSize += [fileDictionary fileSize];
}

return fileSize;

问题:

  1. 有没有可用的内置函数?

  2. 如果不是,计算大小的最佳方法是什么?

  3. 用缓存来存储已经计算好的文件大小好不好?

谢谢...

【问题讨论】:

    标签: xcode macos cocoa


    【解决方案1】:

    您可以使用stat

    -(unsigned long long)getFolderSize : (NSString *)folderPath;
    
    {
        char *dir = (char *)[folderPath fileSystemRepresentation];
    DIR *cd;
    
    struct dirent *dirinfo;
    int lastchar;
    struct stat linfo;
    static unsigned long long totalSize = 0;
    
    cd = opendir(dir);
    
    if (!cd) {
        return 0;
    }
    
    while ((dirinfo = readdir(cd)) != NULL) {
        if (strcmp(dirinfo->d_name, ".") && strcmp(dirinfo->d_name, "..")) {
            char *d_name;
    
    
            d_name = (char*)malloc(strlen(dir)+strlen(dirinfo->d_name)+2);
    
            if (!d_name) {
                //out of memory
                closedir(cd);
                exit(1);
            }
    
            strcpy(d_name, dir);
            lastchar = strlen(dir) - 1;
            if (lastchar >= 0 && dir[lastchar] != '/')
                strcat(d_name, "/");
            strcat(d_name, dirinfo->d_name);
    
            if (lstat(d_name, &linfo) == -1) {
                free(d_name);
                continue;
            }
            if (S_ISDIR(linfo.st_mode)) {
                if (!S_ISLNK(linfo.st_mode))
                    [self getFolderSize:[NSString stringWithCString:d_name encoding:NSUTF8StringEncoding]];
                free(d_name);
            } else {
                if (S_ISREG(linfo.st_mode)) {
                    totalSize+=linfo.st_size;
                } else {
                    free(d_name);
                }
            }
        }
    }
    
    closedir(cd);
    
    return totalSize;
    
    }
    

    看看Mac OS X not reporting directory sizes correctly?

    【讨论】:

    • 这与 OP 的方法真的有很大不同吗?
    • @Yatheesha 如果您的目标不是 10.8 osx。您可以使用碳 API。看看Obtain directory size.
    【解决方案2】:
      1. Is there any built in function available?
    

    fileSize 是一个提供大小的内置函数。

      2. If not what is the best way to calculate the size?
    

    此方法足以计算文件夹/目录的大小。

      3. Is it good to use cache to store already calculated file size?
    

    是的,您可以将其存储在缓存中。

    【讨论】:

    • fileSize 仅给出文件夹的内部大小而不是完整大小。
    • 假设文件夹大小为 2Gb。如果我使用 fileSize,我会得到小于 MB 的大小,而不是完整大小。
    猜你喜欢
    • 1970-01-01
    • 2017-02-06
    • 1970-01-01
    • 1970-01-01
    • 2010-11-26
    • 2017-01-16
    • 2011-03-11
    • 2015-12-07
    相关资源
    最近更新 更多