NSURLSession解决下载中的解压缩问题

如果服务器端的文件是压缩包,我们需要将下载的文件进行解压缩到本地,苹果自己封装了一些压缩与解压缩的框架但是大都是C语言的,不太好用。所以我们使用封装好的SSZipArchive框架。

打开框架的SSZipArchive.h文件。所有的框架包括苹果自封的框架越放在最前面的就是作者推荐我们使用 也是经常使用的方法,最常使用的方法如下:

// Unzip 解压缩

+ (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination;

+ (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination overwrite:(BOOL)overwrite password:(NSString *)password error:(NSError **)error;

+ (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination delegate:(id<SSZipArchiveDelegate>)delegate;

+ (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination overwrite:(BOOL)overwrite password:(NSString *)password error:(NSError **)error delegate:(id<SSZipArchiveDelegate>)delegate;

// Zip 压缩的方法

+ (BOOL)createZipFileAtPath:(NSString *)path withFilesAtPaths:(NSArray *)filenames;

+ (BOOL)createZipFileAtPath:(NSString *)path withContentsOfDirectory:(NSString *)directoryPath;


- (id)initWithPath:(NSString *)path;

- (BOOL)open;

- (BOOL)writeFile:(NSString *)path;

- (BOOL)writeData:(NSData *)data filename:(NSString *)filename;

- (BOOL)close;

使用步骤:将框架导入工程,Command+B进行编译发现很多错误。大部分第三方框架都需要libz静态库的支持,SSZipArchive压缩与解压缩的框架也不例外。导入静态库步骤如下:


再次编译,错误消失。

具体代码如下:

<span style="font-size:18px;">//
//  ViewController.m
//  04-URLSession的下载
//
//  Created by apple on 15/1/23.
//  Copyright (c) 2015年 apple. All rights reserved.
/**
 NSURLSession下载,默认将下载的文件保存到tmp目录下。如果回调方法什么事情都没做。tmp里面的东西会自动删除
 
 比如:通常从服务器下载的是压缩包(压缩包会省流量).
 
 当文件下载完成以后,会解压缩。 原始的zip包不需要了。
 
 解压缩也是耗时操作。也需要在子线程执行
 */

#import "ViewController.h"
#import "SSZipArchive.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
    // 1. url
    NSString *urlStr = @"http://127.0.0.1/itcast/images.zip";
    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSURL *url = [NSURL URLWithString:urlStr];
    
    // 2. 下载
    [[[NSURLSession sharedSession] downloadTaskWithURL:url completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
        
        NSLog(@"文件的路径%@", location.path);
        
        NSString *cacheDir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        NSLog(@"%@", cacheDir);
        /**
         FileAtPath:要解压缩的文件
         Destination: 要解压缩到的路径
         */
        [SSZipArchive unzipFileAtPath:location.path toDestination:cacheDir];
        
    }] resume];
}

@end</span>
将程序中block块的代码都屏蔽。先运行一下,打开沙盒路径。点击屏幕发现在临时文件夹temp中多了一个文件,过了几秒又消失了。如下所示:


说明在内存峰值等诸多方面,NSSession已经为我们做好了。我们只需要为NSURLSession添加任务即可。

取消屏蔽。利用第三方框架SSZipArchive将服务器端的压缩包下载并解压到指定的本地路径。运行结果如下:


解压缩到本地最重要的是掌握框架SSZipArchive中

+ (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination;方法的用法。

FileAtPath:是本地中需要解压缩的文件(全路径加文件名)

destination:指将压缩包解压后的文件存放的位置(纯路径,无文件名)。

还要重要掌握NSURLSession的如下用法(为解压缩专用):

[[[NSURLSessionsharedSession] downloadTaskWithURL:urlcompletionHandler:^(NSURL *location,NSURLResponse *response, NSError *error) {

}] resume];

其中location有个path属性,location.path指将服务器端文件(一般指压缩包)下载到本地的路径也就是刚才沙盒temp文件夹下的文件,过一段时间就会自动被删除,不用我们再单独再做删除压缩包等额外的处理。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值