第一种封装:

-(NSInteger)getSizeOfFilePath:(NSString *)filePath{
/** 定义记录大小 */
NSInteger totalSize = ;
/** 创建一个文件管理对象 */
NSFileManager * manager = [NSFileManager defaultManager];
/**获取文件下的所有路径包括子路径 */
NSArray * subPaths = [manager subpathsAtPath:filePath];
/** 遍历获取文件名称 */
for (NSString * fileName in subPaths) {
/** 拼接获取完整路径 */
NSString * subPath = [filePath stringByAppendingPathComponent:fileName];
/** 判断是否是隐藏文件 */
if ([fileName hasPrefix:@".DS"]) {
continue;
}
/** 判断是否是文件夹 */
BOOL isDirectory;
[manager fileExistsAtPath:subPath isDirectory:&isDirectory];
if (isDirectory) {
continue;
}
/** 获取文件属性 */
NSDictionary *dict = [manager attributesOfItemAtPath:subPath error:nil];
/** 累加 */
totalSize += [dict fileSize]; }
/** 返回 */
return totalSize;
}

第二种  block:

/** 根据文件路径删除文件 */
+(void)removeDirectoryPath:(NSString *)directoryPath{ /** 创建文件管理者 */
NSFileManager * manager = [NSFileManager defaultManager];
/** 判断文件路径是否存在 */
BOOL isDirectory;
BOOL isExist = [manager fileExistsAtPath:directoryPath isDirectory:&isDirectory];
if (!isDirectory||!isExist) {
/** 提示错误信息 */
@throw [NSException exceptionWithName:NSStringFromClass(self) reason:@"文件路径错误!" userInfo:nil];
}
/** 删除文件 */
[manager removeItemAtPath:directoryPath error:nil];
/** 创建文件 */
[manager createDirectoryAtPath:directoryPath withIntermediateDirectories:YES attributes:nil error:nil];
}
/** 根据文件路径获取文件大小 */
+(void)getSizeOfFilePath:(NSString *)filePath completion:(void (^)(NSInteger totalSize))completion{ /** 创建文件管理者 */
NSFileManager * manager = [NSFileManager defaultManager];
/** 判断文件路径是否存在 */
BOOL isDirectory;
BOOL isExist = [manager fileExistsAtPath:filePath isDirectory:&isDirectory];
if (!isDirectory||!isExist) {
/** 提示错误信息 */
@throw [NSException exceptionWithName:NSStringFromClass(self) reason:@"文件路径错误!" userInfo:nil];
}
/** 开启子线程因以下是耗时操作 */
dispatch_async(dispatch_get_global_queue(, ), ^{
/** 定义记录文件大小 */
NSInteger totalSize = ; /** 获取文件 */
NSArray * subPaths = [manager subpathsAtPath:filePath];
/** 遍历文件 */
for (NSString * fileName in subPaths) {
/** 拼接完整路径 */
NSString * subPath = [filePath stringByAppendingPathComponent:fileName];
/** 判断是否是隐藏.DS_Store */
if ([subPath hasPrefix:@".DS"]) {
continue;
}
/** 判断是否是文件夹 */
BOOL isDirectory;
[manager fileExistsAtPath:subPath isDirectory:&isDirectory];
if (isDirectory) {
continue;
}
/** 获取文件属性 */
NSDictionary * dic = [manager attributesOfItemAtPath:subPath error:nil];
totalSize += [dic fileSize];
}
/** 回到主线程 */
dispatch_sync(dispatch_get_main_queue(), ^{
/** block不为空 */
if (completion) {
completion(totalSize);
}
});
}); }

oc 根据文件路径获取文件大小的更多相关文章

  1. OC NSFileManager(文件路径操作)

    OC NSFileManager(文件路径操作) 初始化 NSFileManager * fm = [NSFileManager defaultManager]; 获取当前目录 [fm current ...

  2. [WinAPI] API 10 [创建、打开、读写文件,获取文件大小]

    在Windows系统中,创建和打开文件都是使用API函数CreateFile,CreateFile通过指定不同的参数来表示是新建一个文件,打开已经存在的文件,还是重新建立文件等.读写文件最为直接的方式 ...

  3. c++从文件路径获取目录

    场景 c++从文件路径获取目录 实现代码 初始化是不正确的,因为需要转义反斜杠: string filename = "C:\\MyDirectory\\MyFile.bat"; ...

  4. 【原创】ABAP根据文件路径获取文件所在目录(续)

    在上一篇文章<ABAP根据文件路径获取文件所在目录>中,我主要的思路是采用 “SPLIT dobj AT sep INTO TABLE result_tab” 句型将文件全路径按分隔符“\ ...

  5. C# Path 有关于文件路径获取的问题 的方法

    string Current = Directory.GetCurrentDirectory();//获取当前根目录 //private string strFilePath = Applicatio ...

  6. ios 关于文件操作 获取 文件大小

     分类: Apple IPhone2012-06-28 11:31 4664人阅读 评论(0) 收藏 举报 ios语言manager测试c c语言 实现 #include "sys/stat ...

  7. java项目部署后的文件路径获取

    //eclipse部署工程 String path = request.getServletContext().getRealPath( File.separator+ "WEB-INF&q ...

  8. 【原创】ABAP根据文件路径获取文件所在目录

    *&---------------------------------------------------------------------* *& Form frm_get_pat ...

  9. 设置下载文件路径 & 获取接口结尾名称。

    // 获取下载位置 private String isExistDir(String saveDir) throws IOException { File downloadFile = new Fil ...

随机推荐

  1. 解决WEB页面上"焦点控制"一法

    解决WEB页面上"焦点控制"一法 分类: Html/Css2011-11-11 17:28 125人阅读 评论(0) 收藏 举报 webjavascriptasp.netbutto ...

  2. GetModuleHandle,AfxGetInstanceHandle使用区别

    当一个文件被映射到调用进程的地址空间时,GetModuleHandle函数得到其中某一模块的句柄. 使用GetModuleHandle函数格式:HMODULE GetModuleHandle( LPC ...

  3. 使用httpwatch抓包

    httpwatch抓包工具是MS的ie自带的一个插件. 但是千里之行始于足下,所以先来利用httpwatch抓一些包来分析分析. 打开IE,快捷键shift+F2打开httpwatch. 如下图: 本 ...

  4. 让app在ios6上具有ios7的扁平效果

    使用cocoapods在工程中加入UI7Kit,关于UI7Kit请自行google. 加入到工程 如果没安装cocoapods,则安装.(http://www.cocoapods.org) 安装方法: ...

  5. 关于视觉里程计以及VI融合的相关研究(长期更新)

    1. svo 源码:https://github.com/uzh-rpg/rpg_svo 国内对齐文章源码的研究: (1)冯斌: 对其代码重写 https://github.com/yueying/O ...

  6. Zepto 使用中的一些注意点

    Zepto 只针对移动端浏览器编写,因此体积更小.效率更高,更重要的是,它的 API 完全仿照 jQuery ,所以学习成本也很低. 但是在开发过程中,我发现 Zepto 还远未成熟,其中包含了一些或 ...

  7. 用HTML5、地理定位API和Web服务来开发移动应用

    HTML 5 是一项让人振奋的技术,这有着充分的理由.这将会是一次技术突破,因为它可以将桌面应用程序功能带入浏览器中.除了传统浏览器外,对于移动浏览器,其潜力甚至更大.不仅如此,最流行的移动浏览器甚至 ...

  8. HashMap为什么线程不安全(hash碰撞与扩容导致)

    一直以来都知道HashMap是线程不安全的,但是到底为什么线程不安全,在多线程操作情况下什么时候线程不安全? 让我们先来了解一下HashMap的底层存储结构,HashMap底层是一个Entry数组,一 ...

  9. C# 整个网页保存成图片

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  10. UVA 1569 Multiple

    题意: 给定m个1位数字,要求用这些数字组成n的倍数的最小数字,如果无法组成就输出0 分析: BFS,由于n最大5000,余数最多5000,利用余数去判重,并记录下路径即可 代码: #include ...