第一种封装:

-(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. Singleton 单例模式 泛型 窗体控制

    MDI子窗体 控制单例 /// <summary> /// 单例提供者 /// </summary> /// <typeparam name="T"& ...

  2. 搭讪培训班 - 名品试用 - YOKA时尚论坛 - YOKA社区

    搭讪培训班 - 名品试用 - YOKA时尚论坛 - YOKA社区 搭讪培训班 发贴回复 发新话题 发布投票 搭讪培训班   1330 1 阅读 回复 跳转到指定楼层 加为好友 时尚懒洋洋 工作:无业游 ...

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

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

  4. js实现车轮的来回滚动

    最近喜欢用js做车轮的来回滚动,简单的js动画分享给大家.有什么建议记得说出来大家一起讨论哦!效果图如下: 源代码: <style> #pic1{ width:20px; height:2 ...

  5. C#接口的使用【转】

    我们定义一个接口public interface IBark{   void Bark();}再定义一个类,继承于IBark,并且必需实现其中的Bark()方法public class Dog:IBa ...

  6. [caffe]深度学习之图像分类模型AlexNet解读

    在imagenet上的图像分类challenge上Alex提出的alexnet网络结构模型赢得了2012届的冠军.要研究CNN类型DL网络模型在图像分类上的应用,就逃不开研究alexnet.这是CNN ...

  7. Newton‘ method 的优缺点

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzE1Mjg5NQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  8. POJ 3311 Hie with the Pie(状压DP + Floyd)

    题目链接:http://poj.org/problem?id=3311 Description The Pizazz Pizzeria prides itself in delivering pizz ...

  9. 彩虹vpn免费10分钟

    账号:rainbowvpn password:10fenzhong server地址:t.chqvpn.com l2tp密钥:123456

  10. CSS3中的弹性流体盒模型技术详解

    先回顾一下CSS1 和 CSS2中都已经定义了哪些布局方面的属性,这样也会增加我们理解弹性布局.   其实我们现在有很多一部分人,你们刚刚接触CSS层叠样式表,或者接触有一段时间了,但是却没有很好的去 ...