获取文件或文件夹大小
2012-10-23 17:55
344 查看
c语言 实现
#include "sys/stat.h"
- (long long) fileSizeAtPath:(NSString*) filePath{
struct stat st;
if(lstat([filePath cStringUsingEncoding:NSUTF8StringEncoding], &st) == 0){
return st.st_size;
}
return 0;
}
objective-c 语言实现
-(long long) fileSizeAtPath:(NSString*) filePath{
NSFileManager* manager = [NSFileManager defaultManager];
if ([manager fileExistsAtPath:filePath]){
return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
}
return 0;
}
其中C语言的性能比OC要高得多。
获取目录大小
#include "sys/stat.h"
- (long long) fileSizeAtPath:(NSString*) filePath{
struct stat st;
if(lstat([filePath cStringUsingEncoding:NSUTF8StringEncoding], &st) == 0){
return st.st_size;
}
return 0;
}
objective-c 语言实现
-(long long) fileSizeAtPath:(NSString*) filePath{
NSFileManager* manager = [NSFileManager defaultManager];
if ([manager fileExistsAtPath:filePath]){
return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
}
return 0;
}
其中C语言的性能比OC要高得多。
获取目录大小
- (long long)folderSize:(const char *)folderPath { long long folderSize = 0; DIR* dir = opendir(folderPath); if (dir == NULL) { return 0; } struct dirent* child; while ((child = readdir(dir)) != NULL) { if (child->d_type == DT_DIR && (child->d_name[0] == '.' && child->d_name[1] == 0)) { continue; } if (child->d_type == DT_DIR && (child->d_name[0] == '.' && child->d_name[1] == '.' && child->d_name[2] == 0)) { continue; } int folderPathLength = strlen(folderPath); char childPath[1024]; stpcpy(childPath, folderPath); if (folderPath[folderPathLength - 1] != '/'){ childPath[folderPathLength] = '/'; folderPathLength++; } stpcpy(childPath + folderPathLength, child->d_name); childPath[folderPathLength + child->d_namlen] = 0; if (child->d_type == DT_DIR){ folderSize += [self folderSize:childPath]; struct stat st; if (lstat(childPath, &st) == 0) { folderSize += st.st_size; } } else if (child->d_type == DT_REG || child->d_type == DT_LNK){ struct stat st; if (lstat(childPath, &st) == 0) { folderSize += st.st_size; } } } return folderSize; }
相关文章推荐
- 获取某文件沙盒路径 删除对应文件 获取文件夹总大小
- C#获取文件和文件夹大小
- 【Powershell】【文件管理】获取文件夹大小和排序 层级关系\大小\名称
- C#获取文件夹/文件的大小以及占用空间
- 创建文件夹,文件,获取文件大小,文件名称
- Windows开发--获取文件大小及获取文件夹大小的方法
- Asp.Net 文件操作基类(读取,删除,批量拷贝,删除,写入,获取文件夹大小,文件属性,遍历目录)(二)
- 通过文件夹文件获取文件夹大小
- 获取文件或是文件夹的大小和占用空间
- python 获取文件夹下所有文件大小
- java 获取文件夹大小,文件大小,文件个数
- java 获取文件夹大小,文件大小,文件个数
- python获取文件及文件夹大小
- iOS获取文件和文件夹大小
- java 获取文件夹大小,文件大小,文件个数
- Asp.Net 文件操作基类(读取,删除,批量拷贝,删除,写入,获取文件夹大小,文件属性,遍历目录)
- android 获取文件夹、文件的大小 以B、KB、MB、GB 为单位
- java 获取文件夹大小、文件大小、文件个数
- [导入]Asp.Net 文件操作基类(读取,删除,批量拷贝,删除,写入,获取文件夹大小,文件属性,遍历目录)(一)
- [代码]Delphi实现获取文件及文件夹大小(支持超过2G的大文件)