最近的項(xiàng)目中涉及到了將zip文件從服務(wù)上下載下來章鲤,然后解壓使用税产。搜索了一下發(fā)現(xiàn)有一個(gè)壓縮與解壓zip文件的第三方“SSZipArchive”:https://github.com/ZipArchive/ZipArchive 是用C語(yǔ)言實(shí)現(xiàn)的怕轿,包裝用于OC與swift.
一、在使用過程中遇到過幾個(gè)坑:
- 導(dǎo)入頭文件沖突撞羽,我在pch文件里面導(dǎo)入了一些OC的頭文件阐斜,而SSZipArchive是由C語(yǔ)言實(shí)現(xiàn)的诀紊,所以報(bào)了很多的系統(tǒng)錯(cuò)誤。解決辦法:將pch里面的導(dǎo)入頭文件代碼放在
"#ifdef OBJC
//導(dǎo)入頭文件
"#endif 里面"
或者刪除里面導(dǎo)入頭文件的代碼邻奠,去具體需要的文件里面導(dǎo)入,有一點(diǎn)暴力哈碌宴。 - 我每一次下載的文件樣式都是一樣的杀狡,所以希望覆蓋式的解壓,一開始沒有注意以為它只有解壓方法:+ (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination;
就自己去判定是否存在然后刪除贰镣,后來去仔細(xì)的看源碼才發(fā)現(xiàn)它是有帶是否覆蓋式解壓的方法:+ (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination overwrite:(BOOL)overwrite password:(NSString *)password error:(NSError * *)error;
當(dāng)然它還有很多方法呜象,包括帶有代理方法,帶有密碼八孝,帶有完成后的block回調(diào)方法董朝,
http://blog.csdn.net/zhengang007/article/details/51019479
這里有每一個(gè)方法的詳細(xì)說明。
二干跛、我的實(shí)現(xiàn):
- (void)downFileFromServer{
//遠(yuǎn)程地址
NSURL *URL = [NSURL URLWithString:DOWN_URL];
//默認(rèn)配置
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
//AFN3.0+基于封住URLSession的句柄
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
//請(qǐng)求
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
//下載Task操作
_downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
// 下載進(jìn)度
} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
//- block的返回值, 要求返回一個(gè)URL, 返回的這個(gè)URL就是文件的位置的路徑
NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [cachesPath stringByAppendingPathComponent:response.suggestedFilename];
return [NSURL fileURLWithPath:path];
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
//設(shè)置下載完成操作
// filePath就是你下載文件的位置子姜,你可以解壓,也可以直接拿來使用
NSString *imgFilePath = [filePath path];// 將NSURL轉(zhuǎn)成NSString
MyLog(@"imgFilePath = %@",imgFilePath);
NSArray *documentArray = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *path = [[documentArray lastObject] stringByAppendingPathComponent:@"Preferences"];
[self releaseZipFilesWithUnzipFileAtPath:imgFilePath Destination:path];
}];
[_downloadTask resume];
}
// 解壓
- (void)releaseZipFilesWithUnzipFileAtPath:(NSString *)zipPath Destination:(NSString *)unzipPath{
NSError *error;
if ([SSZipArchive unzipFileAtPath:zipPath toDestination:unzipPath overwrite:YES password:nil error:&error delegate:self]) {
MyLog(@"success");
MyLog(@"unzipPath = %@",unzipPath);
}else {
MyLog(@"%@",error);
}
}
#pragma mark - SSZipArchiveDelegate
- (void)zipArchiveWillUnzipArchiveAtPath:(NSString *)path zipInfo:(unz_global_info)zipInfo {
MyLog(@"將要解壓楼入。");
}
- (void)zipArchiveDidUnzipArchiveAtPath:(NSString *)path zipInfo:(unz_global_info)zipInfo unzippedPath:(NSString *)unzippedPat uniqueId:(NSString *)uniqueId {
MyLog(@"解壓完成哥捕!");
}
當(dāng)然還得遵守協(xié)議:SSZipArchiveDelegate
以上就是我使用SSZipArchive的體會(huì),歡迎各位指正嘉熊。