计算可可中的目录大小 [英] calculating directory size in cocoa
本文介绍了计算可可中的目录大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想计算目录(文件夹)大小,我必须列出所有的文件和文件夹(子文件夹)在一个卷(驱动器)与其相应的大小。我使用下面的代码来计算大小。这个问题代码是性能问题。我使用 NSBrowser
显示。
NSArray * filesArray = [[ NSFileManager defaultManager] subpathsOfDirectoryAtPath:folderPath error:nil];
NSEnumerator * fileEnumerator = [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;
问题:
p>谢谢...
解决方案
您可以使用 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){
//内存不足
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] ];
免费(d_name);
} else {
if(S_ISREG(linfo.st_mode)){
totalSize + = linfo.st_size;
} else {
free(d_name);
}
}
}
}
closedir(cd);
return totalSize;
}
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 .
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:
Is there any built in function available?
If not what is the best way to calculate the size?
Is it good to use cache to store already calculated file size?
Thanks...
解决方案
You can use 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?
这篇关于计算可可中的目录大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文