自定义封装清除缓存与删除子文件夹路径的两个工具类

FileTool.h 文件

#import <Foundation/Foundation.h>

@interface FileTool : NSObject

/*
 *  获取清除缓存用的方法:
 *  @pragma directoryPath 文件夹路径 ;
 
 *  @return 返回文件夹的大小
 */
+ (NSInteger)getFileSize:(NSString *)directoryPath ;


/*
 *  删除文件夹子路径:
 *  @pragma directoryPath 文件夹路径 ;
 *  directoryPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject] ;
 
 *  @return 返回文件夹的大小
 */
+ (void)removeCache:(NSString *)directoryPath ;
@end

FileTool.m 文件

#import "FileTool.h"

@implementation FileTool

#pragma mark - 获取缓存大小
+ (NSInteger)getFileSize:(NSString *)directoryPath {
    //NSFileManager
    //attributesOfItemAtPath:指定文件路径 , 就能获取文件属性:
    //把所有的文件尺寸加起来!
    //获取沙盒目录中的Cache文件夹的位置:
    //NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject] ;
    //在Cache文件夹下找到想要的文件夹位置:
    //NSString *defaultPath = [cachePath stringByAppendingPathComponent:@"default"] ;
    //attributesOfItemAtPath:这个方法只能获取文件的大小 , 不能获取文件夹里所有的文件的大小!所以要遍历求和!
    // 获取文件管理者:
    NSFileManager *manager = [NSFileManager defaultManager] ;
    //抛异常:
    BOOL isDirectory ;
    BOOL isExist = [manager fileExistsAtPath:directoryPath isDirectory:&isDirectory] ;
    if (!isExist || !isDirectory) {
        NSException *exception = [NSException exceptionWithName:@"pathError" reason:@"请检查你写入的地址..." userInfo:nil] ;
        //抛出异常:
        [exception raise] ;
    }

    // 获取文件夹下所有的子路径:  让文件管理者去遍历所有的defalutPath下得子路径 ;
    NSArray *subPath = [manager subpathsAtPath:directoryPath] ;
    
    NSInteger totalSize = 0 ;
    for (NSString *filePath in subPath) {
        //获取文件的全路径:
        NSString *holeFilePath = [directoryPath stringByAppendingPathComponent:filePath] ;
        
        //文件的全路径包含:1.所有文件,2.文件夹,3.隐藏文件:
        //判断是否为隐藏文件:
        if ([holeFilePath containsString:@".DS"]) continue ;
        //判断是否为文件夹:
        BOOL isDirectory ;
        //判断是否文件存在,是否是个文件夹?!
        BOOL isExist = [manager fileExistsAtPath:holeFilePath isDirectory:&isDirectory] ;
        //如果文件不存在,或是个文件夹:
        if (!isExist || isDirectory) continue ;
        //获取全路径文件属性:
        NSDictionary *attrs = [manager attributesOfItemAtPath:holeFilePath error:nil] ;
        //defaultPath的大小:
        NSInteger fileSize = [attrs fileSize] ;
        //每次遍历每次++ :
        totalSize += fileSize ;
    }
    NSLog(@"%ld" , totalSize) ;
    return totalSize ;
}


+ (void)removeCache:(NSString *)directoryPath {
    //清空缓存:
    NSFileManager *manager = [NSFileManager defaultManager] ;
    BOOL isDirectory ;
    BOOL isExist = [manager fileExistsAtPath:directoryPath isDirectory:&isDirectory] ;
    if (!isExist || !isDirectory) {
        NSException *exception = [NSException exceptionWithName:@"pathError" reason:@"请检查你写入的地址..." userInfo:nil] ;
        //抛出异常:
        [exception raise] ;
    }
    //获取文件夹下的二级路径 , 不包括更深层的路径:
    NSArray *subPath = [manager contentsOfDirectoryAtPath:directoryPath error:nil] ;
    NSLog(@"%@" , subPath) ;
    for (NSString *filePath in subPath) {
        NSString *holeFilePath = [directoryPath stringByAppendingPathComponent:filePath] ;
        [manager removeItemAtPath:holeFilePath error:nil] ;
    }
}
@end

愿编程让这个世界更美好

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,645评论 25 708
  • ## 可重入函数 ### 可重入性的理解 若一个程序或子程序可以安全的被并行执行,则称其为可重入的;即当该子程序正...
    夏至亦韵阅读 730评论 0 0
  • 在那甜瓜成熟的季节二百零七,扫描二维码了解小说更多细节:刘万红是和李海平以及刘红叶同班同学,只是刘万红本身不爱说话...
    思想聚焦的原创阅读 148评论 2 6
  • NCE-2 34 Quick work Origin Content Lesson 34 Quick work 破...
    愿景力阅读 288评论 0 0
  • 平日里和三五好友聊天,如果是股票、基金、国债什么的,我一定是困得要死,从桌子上一直出溜到地下;如果是车子、房子、孩...
    婴儿看世界阅读 336评论 13 3