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

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

作者: 小苗晓雪 | 来源:发表于2017-03-27 21:41 被阅读14次

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

愿编程让这个世界更美好

相关文章

网友评论

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

      本文链接:https://www.haomeiwen.com/subject/unhdottx.html