iOS中下载压缩包并解压

总结一下之前项目用到的知识点,关于下载的文件是压缩包,我们下载到本地后,用SSZipArchive这个第三方进行解压,然后将解压后的文件放到指定目录下,再进行操作。详细代码如下:

- (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; 当前已经下载的大小
        
        // 给Progress添加监听 KVO
        JJLog(@"这个进度为%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:@"当前进度为:%.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转成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地址。
最后祝大家工作愉快~

你可能感兴趣的:(iOS中下载压缩包并解压)