zoukankan      html  css  js  c++  java
  • 处理文件缓存

    处理文件缓存

     

    #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



     

  • 相关阅读:
    视频质量评测标准——VMAF
    净化网络环境!可信数字内容版权服务解决方案发布
    如何用sysbench做好IO性能测试
    云栖专辑|阿里开发者们的第二个感悟:PG大V德哥的使命感与开放心态
    CRI 与 ShimV2:一种 Kubernetes 集成容器运行时的新思路
    阿里巴巴持续投入,etcd 正式加入 CNCF
    阿里系统软件迎战“双11”超高流量峰值全纪录
    从SQL Server CloudDBA 看云数据库智能化
    ls -l 权限后面有个点
    Error File: /admin/app/template.app.php at 285 line.
  • 原文地址:https://www.cnblogs.com/liuzhenjie/p/5501966.html
Copyright © 2011-2022 走看看