iOS-常用工具库代码

//一.对文件和文件夹处理

//功能:获取文件夹大小,判断缓存是否过期,超出预设容量,

//备注:文件夹的名字是url通过MD5,Hash得到的字符串。

#pragma mark - Cache Helper
- (NSString *)filePathFromRequest:(NSURLRequest *)request isInfo:(BOOL)info{
    NSString *url = request.URL.absoluteString;
    NSString *fileName = [self cacheRequestFileName:url];
    NSString *otherInfoFileName = [self cacheRequestOtherInfoFileName:url];
    NSString *filePath = [self cacheFilePath:fileName];
    NSString *fileInfoPath = [self cacheFilePath:otherInfoFileName];
    if (info) {
        return fileInfoPath;
    }
    return filePath;
}

- (NSString *)cacheRequestFileName:(NSString *)requestUrl {
    return [self md5Hash:[NSString stringWithFormat:@"%@",requestUrl]];
}
- (NSString *)cacheRequestOtherInfoFileName:(NSString *)requestUrl {
    return [self md5Hash:[NSString stringWithFormat:@"%@-otherInfo",requestUrl]];
}
- (NSString *)cacheFilePath:(NSString *)file {
    NSString *path = [NSString stringWithFormat:@"%@/%@",self.diskPath,self.cacheFolder];
    NSFileManager *fm = [NSFileManager defaultManager];
    BOOL isDir;
    if ([fm fileExistsAtPath:path isDirectory:&isDir] && isDir) {
        //
    } else {
        [fm createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
    }
    NSString *subDirPath = [NSString stringWithFormat:@"%@/%@/%@",self.diskPath,self.cacheFolder,self.subDirectory];
    if ([fm fileExistsAtPath:subDirPath isDirectory:&isDir] && isDir) {
        //
    } else {
        [fm createDirectoryAtPath:subDirPath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    NSString *cFilePath = [NSString stringWithFormat:@"%@/%@",subDirPath,file];
//    NSLog(@"%@",cFilePath);
    return cFilePath;
}

//清除自建的缓存目录
- (void)checkCapacity {
    if ([self folderSize] > self.diskCapacity) {
        [self deleteCacheFolder];
    }
}
- (void)deleteCacheFolder {
    [[NSFileManager defaultManager] removeItemAtPath:[self cacheFolderPath] error:nil];
}
- (NSUInteger)folderSize {
    NSArray *filesArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:[self cacheFolderPath] error:nil];
    NSEnumerator *filesEnumerator = [filesArray objectEnumerator];
    NSString *fileName;
    unsigned long long int fileSize = 0;
    while (fileName = [filesEnumerator nextObject]) {
        NSDictionary *fileDic = [[NSFileManager defaultManager] attributesOfItemAtPath:[[self cacheFolderPath] stringByAppendingPathComponent:fileName] error:nil];
        fileSize += [fileDic fileSize];
    }
    
    return (NSUInteger)fileSize;
}
#pragma mark - Function Helper
- (NSString *)md5Hash:(NSString *)str {
    const char *cStr = [str UTF8String];
    unsigned char result[16];
    CC_MD5( cStr, (CC_LONG)strlen(cStr), result );
    NSString *md5Result = [NSString stringWithFormat:
                           @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
                           result[0], result[1], result[2], result[3],
                           result[4], result[5], result[6], result[7],
                           result[8], result[9], result[10], result[11],
                           result[12], result[13], result[14], result[15]
                           ];
    return md5Result;
}
- (NSString *)cacheFolderPath {
    return [NSString stringWithFormat:@"%@/%@/%@",self.diskPath,self.cacheFolder,self.subDirectory];
}

#pragma mark - Getter
- (NSString *)diskPath {
    if (!_diskPath) {
        _diskPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    }
    return _diskPath;
}
- (NSMutableDictionary *)responseDic {
    if (!_responseDic) {
        _responseDic = [NSMutableDictionary dictionaryWithCapacity:0];
    }
    return _responseDic;
}
- (NSString *)cacheFolder {
    if (!_cacheFolder) {
        _cacheFolder = @"Url";
    }
    return _cacheFolder;
}
- (NSString *)subDirectory {
    if (!_subDirectory) {
        _subDirectory = @"UrlCacheDownload";
    }
    return _subDirectory;
}
- (NSUInteger)memoryCapacity {
    if (!_memoryCapacity) {
        _memoryCapacity = 20 * 1024 * 1024;
    }
    return _memoryCapacity;
}
- (NSUInteger)diskCapacity {
    if (!_diskCapacity) {
        _diskCapacity = 200 * 1024 * 1024;
    }
    return _diskCapacity;
}
- (NSUInteger)cacheTime {
    if (!_cacheTime) {
        _cacheTime = 0;
    }
    return _cacheTime;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值