zoukankan      html  css  js  c++  java
  • 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];
            }
            
        }
        ;
    }

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

  • 相关阅读:
    233. Number of Digit One
    232. Implement Queue using Stacks
    231. Power of Two
    230. Kth Smallest Element in a BST
    229. Majority Element II
    228. Summary Ranges
    227. Basic Calculator II
    ArcGIS 网络分析[3] 发布NAServer到ArcGIS for Server(以Server 10.4为例)
    iView的使用【小白向】
    Vuejs环境安装与工程建立【小白Windows向】
  • 原文地址:https://www.cnblogs.com/sunjianfei/p/5781799.html
Copyright © 2011-2022 走看看