總結(jié)一下之前項目用到的知識點斥扛,關(guān)于下載的文件是壓縮包逞度,我們下載到本地后,用SSZipArchive這個第三方進(jìn)行解壓工闺,然后將解壓后的文件放到指定目錄下桥氏,再進(jìn)行操作温峭。詳細(xì)代碼如下:
- (void)createProgress {
//[NSString stringWithFormat:@"%@%@",DOWNLOADZHENGSHUURL,@"1097449557765533696489"]
NSURL *URL = [NSURL URLWithString:_downloadUrl];
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
//AFN3.0+基于封住URLSession的句柄
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
//請求
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
[MBProgressHUD showMessage:nil toView:self.view];
//下載Task操作
_downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
if (downloadProgress) {
JJLog(@"123");
}
// @property int64_t totalUnitCount; 需要下載文件的總大小
// @property int64_t completedUnitCount; 當(dāng)前已經(jīng)下載的大小
// 給Progress添加監(jiān)聽 KVO
JJLog(@"這個進(jìn)度為%f",1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount);
// 回到主隊列刷新UI
dispatch_async(dispatch_get_main_queue(), ^{
self.progressView.progress = 1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount;
_titlelabel.text = [NSString stringWithFormat:@"當(dāng)前進(jìn)度為:%.2f%%",(1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount) * 100];
});
} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
//- block的返回值, 要求返回一個URL, 返回的這個URL就是文件的位置的路徑
NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
JJLog(@"tttss == %@", cachesPath);
NSString *path = [cachesPath stringByAppendingPathComponent:response.suggestedFilename];
return [NSURL fileURLWithPath:path];
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
// filePath就是你下載文件的位置,你可以解壓字支,也可以直接拿來使用
[MBProgressHUD hideHUDForView:self.view];
NSString *imgFilePath = [filePath path];// 將NSURL轉(zhuǎn)成NSString
// UIImage *img = [UIImage imageWithContentsOfFile:imgFilePath];
JJLog(@"img == %@", imgFilePath);
NSString *zipPath = imgFilePath;
//獲取沙盒doucument路徑
NSArray*pathsss =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *documentsDirectory = [pathsss objectAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString*testDirectory = [documentsDirectory stringByAppendingPathComponent:@"test"];
//刪除
[[NSFileManager defaultManager] removeItemAtPath:testDirectory error:nil];
[fileManager createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:nil];
JJLog(@"test == %@",testDirectory); //解壓后的路徑
//解壓
[SSZipArchive unzipFileAtPath:zipPath toDestination:testDirectory];
NSArray *filename = [Tool getFilenamelistOfType:@"pdf" fromDirPath:testDirectory];
NSArray *fileArray;
if (filename.count > 0) {
NSString *stringOne = [testDirectory stringByAppendingString:[NSString stringWithFormat:@"/%@",filename[0]]];
NSString *stringTwo = [testDirectory stringByAppendingString:[NSString stringWithFormat:@"/%@",filename[1]]];
JJLog(@"stringone == %@", stringOne);
fileArray = @[stringOne,stringTwo];
}
if (filename.count > 0) {
_downloadView = [[DownloadView alloc] initWithFrame:self.view.frame NSArray:fileArray];
[self.view addSubview:_downloadView];
__weak typeof(self) weakSelf = self;
_downloadView.delegate = self;
_downloadView.cancelBtn = ^() {
[weakSelf.downloadView removeFromSuperview];
_downloadView = nil;
};
} else {
// [self cancelDownload];
[Tool HUDShowAddedTo:self.view withTitle:@"下載失敗" delay:1.5];
}
}];
[_downloadTask resume];
}
點擊獲取SSZipArchive地址凤藏。
最后祝大家工作愉快~