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

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

posted @ 2016-08-17 20:33  有棱角的圆  阅读(604)  评论(0编辑  收藏  举报