zip压缩和解压缩

本文介绍如何利用ZipArchive第三方框架下载并解压iOS框架。首先从指定地址下载ZipArchive,解决可能遇到的错误,然后通过代码示例展示如何下载zip文件、解压并移除原始压缩包。

Zipachieve第三方框架下载地址:

https://code.google.com/archive/p/ziparchive/downloads

 

1.下载完成后解压,将文件拉入项目中,这是可能会报错;

2.添加libz.1.2.5.tbd,此时运行一下,要是仍然有错可能是ARC的问题,修改不兼容ARC(-fno-objc-arc);

3.上代码:

下载一个zip文件:

NSURL *url=[NSURL URLWithString:@"http://192.168.1.102:8080/zip/JFTestFramework.framework.zip"];
    NSMutableURLRequest *re=[NSMutableURLRequest requestWithURL:url];
    
    [NSURLConnection sendAsynchronousRequest:re queue:[NSOperationQueue new] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        if (data) {
            NSLog(@"%ld",data.length);
             [self performSelectorOnMainThread:@selector(saveFramework:) withObject:data waitUntilDone:YES];
        }else{
            
            NSLog(@"加载失败");
        }
       
    }];

 

 

保存下载的压缩包,解压压缩包,并删除原来的压缩包:

-(void)saveFramework:(NSData*)data{
    NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *zipPath = [NSString stringWithFormat:@"%@/%@",documentDirectory,@"framework.zip"];
    NSLog(@"1---------%@",zipPath);
   // NSLog(@"%@",data);
   
    NSString *bundlePath = [NSString stringWithFormat:@"%@/%@",documentDirectory,@""];
    [data writeToFile:zipPath atomically:YES];
    
    ZipArchive *zip=[[ZipArchive alloc]init];
    if ([zip UnzipOpenFile:zipPath]) {
        NSLog(@"打开成功");
        if ([zip UnzipFileTo:bundlePath overWrite:YES]) {
            NSLog(@"解压成功");
            [[NSFileManager defaultManager]removeItemAtPath:zipPath error:nil];
            [zip UnzipCloseFile];
        }
        
    }
    ;
}

此时打开文件下载路径可以看到下载解压之后的文件:

转载于:https://www.cnblogs.com/sunjianfei/p/5781799.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值