码迷,mamicode.com
首页 > 其他好文 > 详细

zip压缩和解压缩

时间:2016-08-17 21:19:24      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:

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];
        }
        
    }
    ;
}

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

技术分享

zip压缩和解压缩

标签:

原文地址:http://www.cnblogs.com/sunjianfei/p/5781799.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!