当前位置: 首页 > news >正文

处理文件缓存

处理文件缓存

 

#import <Foundation/Foundation.h>

 

 

 

@interface LZJFileTool : NSObject

 

/**

 *  获取文件夹尺寸

 *

 *  @param directoryPath 文件夹路径

 *

 *  @return 返回文件夹尺寸

 */

+ (void)getFileSize:(NSString *)directoryPath completion:(void(^)(NSInteger))completion;

 

 

/**

 *  删除文件夹所有文件

 *

 *  @param directoryPath 文件夹路径

 */

+ (void)removeDirectoryPath:(NSString *)directoryPath;

 

 

@end

 



 

#import "LZJFileTool.h"

 

@implementation LZJFileTool

 

+ (void)removeDirectoryPath:(NSString *)directoryPath

{

    // 获取文件管理者

    NSFileManager *mgr = [NSFileManager defaultManager];

    

    BOOL isDirectory;

    BOOL isExist = [mgr fileExistsAtPath:directoryPath isDirectory:&isDirectory];

    

    if (!isExist || !isDirectory) {

        // 抛异常

        // name:异常名称

        // reason:报错原因

        NSException *excp = [NSException exceptionWithName:@"pathError" reason:@" 需要传入的是文件夹路径,并且路径要存在" userInfo:nil];

        [excp raise];

        

    }

    

    // 获取cache文件夹下所有文件,不包括子路径的子路径

    NSArray *subPaths = [mgr contentsOfDirectoryAtPath:directoryPath error:nil];

    

    for (NSString *subPath in subPaths) {

        // 拼接完成全路径

        NSString *filePath = [directoryPath stringByAppendingPathComponent:subPath];

        

        // 删除路径

        [mgr removeItemAtPath:filePath error:nil];

    }

 

}

 

// 自己去计算SDWebImage做的缓存

+ (void)getFileSize:(NSString *)directoryPath completion:(void(^)(NSInteger))completion

{

    

    // 获取文件管理者

    NSFileManager *mgr = [NSFileManager defaultManager];

    BOOL isDirectory;

    BOOL isExist = [mgr fileExistsAtPath:directoryPath isDirectory:&isDirectory];

    

    if (!isExist || !isDirectory) {

        // 抛异常

        // name:异常名称

        // reason:报错原因

       NSException *excp = [NSException exceptionWithName:@"pathError" reason:@" 需要传入的是文件夹路径,并且路径要存在" userInfo:nil];

        [excp raise];

        

    }

    

    dispatch_async(dispatch_get_global_queue(0, 0), ^{

        

        // 获取文件夹下所有的子路径,包含子路径的子路径

        NSArray *subPaths = [mgr subpathsAtPath:directoryPath];

        

        NSInteger totalSize = 0;

        

        for (NSString *subPath in subPaths) {

            // 获取文件全路径

            NSString *filePath = [directoryPath stringByAppendingPathComponent:subPath];

            

            // 判断隐藏文件

            if ([filePath containsString:@".DS"]) continue;

            

            // 判断是否文件夹

            BOOL isDirectory;

            // 判断文件是否存在,并且判断是否是文件夹

            BOOL isExist = [mgr fileExistsAtPath:filePath isDirectory:&isDirectory];

            if (!isExist || isDirectory) continue;

            

            // 获取文件属性

            // attributesOfItemAtPath:只能获取文件尺寸,获取文件夹不对,

            NSDictionary *attr = [mgr attributesOfItemAtPath:filePath error:nil];

            

            // 获取文件尺寸

            NSInteger fileSize = [attr fileSize];

            

            totalSize += fileSize;

        }

        

        // 计算完成回调

        dispatch_sync(dispatch_get_main_queue(), ^{

            if (completion) {

                completion(totalSize);

            }

        });

        

        

 

    });

    

    

}

 

@end



 

转载于:https://www.cnblogs.com/liuzhenjie/p/5501966.html

相关文章:

  • http模块
  • fs模块
  • fs 流
  • Autohotkey读写ini文件内容
  • 静态资源托管
  • 难道.NET Core到R2连中文编码都不支持吗?
  • 前端和后端接口的实现简述
  • DCloud与APICloud的对比
  • nodejs --路由 router
  • 栈的应用——迷宫问题
  • 使用sass的安装使用
  • Linux_Rsync远程同步备份服务器
  • 思索好久要不要写这个react ,还是写了
  • 操作系统原理(转)
  • webpack静态模块打包器,一般不知道但是就打包了
  • CSS魔法堂:Absolute Positioning就这个样
  • ECMAScript入门(七)--Module语法
  • egg(89)--egg之redis的发布和订阅
  • Iterator 和 for...of 循环
  • Javascript Math对象和Date对象常用方法详解
  • js数组之filter
  • Protobuf3语言指南
  • Traffic-Sign Detection and Classification in the Wild 论文笔记
  • 判断客户端类型,Android,iOS,PC
  • 如何合理的规划jvm性能调优
  • 通信类
  • !!java web学习笔记(一到五)
  • #if和#ifdef区别
  • #Spring-boot高级
  • (2)Java 简介
  • (二)基于wpr_simulation 的Ros机器人运动控制,gazebo仿真
  • (十)T检验-第一部分
  • (算法设计与分析)第一章算法概述-习题
  • (转)eclipse内存溢出设置 -Xms212m -Xmx804m -XX:PermSize=250M -XX:MaxPermSize=356m
  • (转)PlayerPrefs在Windows下存到哪里去了?
  • (转)四层和七层负载均衡的区别
  • (转)我也是一只IT小小鸟
  • .dat文件写入byte类型数组_用Python从Abaqus导出txt、dat数据
  • .NET Standard / dotnet-core / net472 —— .NET 究竟应该如何大小写?
  • .NET 依赖注入和配置系统
  • .NET构架之我见
  • .net与java建立WebService再互相调用
  • /var/lib/dpkg/lock 锁定问题
  • @Transactional 详解
  • [ vulhub漏洞复现篇 ] ThinkPHP 5.0.23-Rce
  • [1525]字符统计2 (哈希)SDUT
  • [8-23]知识梳理:文件系统、Bash基础特性、目录管理、文件管理、文本查看编辑处理...
  • [codevs] 1029 遍历问题
  • [Excel] vlookup函数
  • [git] windows系统安装git教程和配置
  • [Java] IDEA Scala环境搭建
  • [Json.net]快速入门
  • [LeetCode]-283. 移动零-1089. 复写零
  • [SAP] ABAP注释快捷键修改
  • [Search Engine] 搜索引擎技术之网络爬虫