第一种封装:

-(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. MFC窗口的父子关系和层级关系

    一直对窗口之间的关系有些混乱,遇到需要指定父窗口的函数时常常要考虑很久,究竟父窗口是哪个窗口,遂上网查资料,略有所悟,简记如下: 对话框中的所有控件(比如Button等)都是其子窗口.        ...

  2. codevs1040 统计单词个数

    题目描述 Description 给出一个长度不超过200的由小写英文字母组成的字母串(约定;该字串以每行20个字母的方式输入,且保证每行一定为20个).要求将此字母串分成k份(1<k<= ...

  3. 【转】关于Activity和Task的设计思路和方法

    Activity和Task是Android Application Framework架构中最基础的应用,开发者必须清楚它们的用法和一些开发技巧.本文用大量的篇幅并通过引用实例的方式一步步深入全面讲解 ...

  4. OpenCV-Python教程(10、直方图均衡化)

    相比C++而言,Python适合做原型.本系列的文章介绍如何在Python中用OpenCV图形库,以及与C++调用相应OpenCV函数的不同之处.这篇文章介绍在Python中使用OpenCV和NumP ...

  5. ExecuteReader(),ExecuteNonQuery(),ExecuteScalar(),ExecuteXmlReader()之间的区别

    本文来自:http://www.cnblogs.com/zhouxiaxue/archive/2006/05/12/398266.html http://www.cnblogs.com/yaoxc/a ...

  6. Android单元測试之JUnit

    随着近期几年測试方面的工作慢慢火热起来.常常看见有招聘測试project师的招聘信息.在Java中有单元測试这么一个JUnit 方式,Android眼下主要编写的语言是Java,所以在Android开 ...

  7. OAuth2.0基本流程

    用户请求客户端>客户端通过在授权服务器上申请的apikey和apisceret>登录访问资源服务器

  8. [Django实战] 第3篇 - 用户认证(初始配置)

    当大家打开一个网站时,第一步做什么?大部分一定是先登录吧,所以我们就从用户认证开始. 打开用户认证 Django本身已经提供了用户认证模块,使用它可以大大简化用户认证模块的开发,默认情况下,用户认证模 ...

  9. 吐槽下CSDN编辑器

    Perface 近期喜欢上了markdown,我认为它就是一些HTML标签的快捷键,用一些符号来取代标签,易学易读易用,何乐而不为呢?近期也喜欢用印象笔记来让我的记忆永存,确实它强大的收集能力让我迷上 ...

  10. RHEL5.8安装Oracle11g

    1.安装环境[root@rusky-oracle11g ~]# uname -r2.6.18-308.el5[root@rusky-oracle11g ~]# cat /etc/issueRed Ha ...