美文网首页IOS 知识积累
iOS中下载压缩包并解压

iOS中下载压缩包并解压

作者: 哈哈大p孩 | 来源:发表于2017-03-15 13:54 被阅读564次

总结一下之前项目用到的知识点,关于下载的文件是压缩包,我们下载到本地后,用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中下载压缩包并解压

    总结一下之前项目用到的知识点,关于下载的文件是压缩包,我们下载到本地后,用SSZipArchive这个第三方进行解...

  • 下载与配置MAVEN

    下载解压 点击链接,下载maven压缩包,并解压 http://maven.apache.org/ 选择Use的D...

  • 使用APICloud封装iOS模块供h5使用开发app

    一、下载APICloud官方的SDK和demo 下载地址 二、解压 继续解压ios压缩包 打开这个工程 三、了解工...

  • Linux下安装JDK

    Linux中安装JDK的步骤很简单,去官网下载压缩包解压并配置一下环境变量就行了。 下载安装并解压 去官网选择对应...

  • 500.【registry】 搭建docker私有仓库

    一、下载 registry 压缩包 二、解压 registry 压缩包,并复制到 /usr/bin/ 目录下 三、...

  • mac下安装pip

    mac下使用sudo easy_install pip会报错。 所以直接下载pip压缩包并解压。下载压缩包:pip...

  • Centos安装JDK1.8

    1、上传JDK压缩包并解压缩 创建/usr/local/java目录,将下载的JDK压缩包放在该目录下,执行解压命...

  • Hive 1.2.2 安装

    1、安装mysql mysql 2、安装hive 下载压缩包并解压 $ wgethttps://archive...

  • Hadoop学习笔记(六)实战wordcount

    配置工程 在maven官网上下载最新的maven压缩包并解压。 下载IntelliJ IDEA并安装。 在IDEA...

  • Mysql免安装配置

    下载 首先在mysql官网 下载mysql 解压配置 解压MySQL压缩包 将以下载的MySQL压缩包解压到自定义...

网友评论

  • 饥人谷_张炜:楼主你好, 请加我QQ504951293 有个问题想请教 谢谢;

本文标题:iOS中下载压缩包并解压

本文链接:https://www.haomeiwen.com/subject/sqklnttx.html