zip壓縮和解壓縮

有稜角的圓發表於2016-08-17

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

此時開啟檔案下載路徑可以看到下載解壓之後的檔案:

相關文章