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地址。
最后祝大家工作愉快~

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,270评论 25 709
  • Ubuntu的发音 Ubuntu,源于非洲祖鲁人和科萨人的语言,发作 oo-boon-too 的音。了解发音是有意...
    萤火虫de梦阅读 99,841评论 9 468
  • 科普 首先推荐一篇文章与 Hadoop 对比,如何看待 Spark 技术里面有很多优秀的回答者,引用用心阁大神的回...
    syncwt阅读 3,276评论 0 1
  • 1. 从 http://www.jetbrains.com/upsource/download/ 下载upsour...
    胡一道阅读 13,102评论 2 4
  • 、人们总是以为通过一些表面现象就好似了解了一个人、其实不去过多的评论才是最基本的尊重、大多数人的喜怒哀乐会展现在一...
    丶乔妹er阅读 2,944评论 0 0