#import "ViewController.h"
#import "SSZipArchive.h"
@interface ViewController ()
@end
@implementation ViewController
/*
NSURLSession下载,默认将下载的文件保存到tmp目录下,如果回调方法里什么事情都没做。
tmp目录里面的东西会自动删除。
*/
- (void)viewDidLoad {
[super viewDidLoad];
//1.url
NSString *str = @"http://localhost/images.zip";
str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:str];
//2.下载
[[[NSURLSession sharedSession] downloadTaskWithURL:url completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSLog(@"文件的路径%@",location.path);
//文件保存到什么地方
NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
//解压文件(文件大会比较耗时)
[SSZipArchive unzipFileAtPath:location.path toDestination:caches];
}] resume];
}
@end
用到的第三方解压缩框架:SSZipArchive
下载地址:https://github.com/samsoffes/ssziparchive
注意:需要引入libz.dylib框架