下载ZIP包解压并加载HTML文件

直接从服务器下载zip文件

 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue, ^{
        NSURL *url = [NSURL URLWithString:@"http://192.168.10.248:30000/test/html_template"];
        NSError *erro = nil;
        NSData *data = [NSData dataWithContentsOfURL:url options:0 error:&erro];
        
        if (!erro) {
            NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
            NSString *zipPath = [path stringByAppendingPathComponent:@"zipfile.zip"];
            
            [data writeToFile:zipPath options:0 error:&erro];
            
            if (!erro) {
                NSLog(@"save zipfile suc");
                [self archZip:zipPath];
            }else{
                NSLog(@"save zipfile fail");
            }
        }else{
            
            NSLog(@"get zipfile fail");
        }

        
    });

将zip文件下载到document目录下,并将此路径获取到,用作解压路径。

解压zip文件

 NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    BOOL archive = [SSZipArchive unzipFileAtPath:zipPath toDestination:path overwrite:YES password:@"123" error:nil];
    if (archive) {
        NSLog(@"archive success");
        
        [[NSFileManager defaultManager] removeItemAtPath:zipPath error:nil];
        
        NSString *pathZip = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
        
        NSString *desPath = [pathZip stringByAppendingPathComponent:@"xxxx/main/main.html"];
        NSString *path = [NSString stringWithFormat:@"file://%@",desPath];
        NSURL *url = [NSURL URLWithString:path];

        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [self.webView loadRequest:request];
    }else{
        
        NSLog(@"archive fail");
    }

使用ZipArchive解压,解压完成后删除原zip包。
之后注意要拼接一个文件路径,也就是手动加上'file://'这个路径头,否则不识别。

你可能感兴趣的:(下载ZIP包解压并加载HTML文件)