简体   繁体   English

在可可中计算目录大小

[英]calculating directory size in cocoa

I want to calculate the directory (folder)size and i have to list all files and folders (subfolders) in a volume(drive) with its corresponding size.I am using the below code to calculate size.The problem with this code is the performance issue . 我想计算目录(文件夹)大小,我必须列出其相应大小的卷(驱动器)中的所有文件和文件夹(子文件夹)。我使用下面的代码来计算大小。此代码的问题是性能问题。 I am using NSBrowser to display . 我正在使用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;

Questions: 问题:

  1. Is there any built in function available? 有内置功能吗?

  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? 使用缓存来存储已经计算的文件大小是否合适?

Thanks... 谢谢...

You can use stat . 你可以使用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;

}

Take a look at Mac OS X not reporting directory sizes correctly? 看看Mac OS X没有正确报告目录大小?

  1. Is there any built in function available?

fileSize is a built-in function that gives you size. fileSize是一个内置函数,可以为您提供大小。

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

This method is good enough to calculate size of a folder/directory. 此方法足以计算文件夹/目录的大小。

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

Yes you can store it in cache. 是的,您可以将其存储在缓存中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM