AFN下载文件解压文件及断点续传

下载文件及断点续传:
http://www.cnblogs.com/qingche/p/5362592.html

下载解压文件:
下载文件用第三方库:AFNetWorking 解压文件用第三方库 : SSZipArchive

#import "DownloadFileManager.h"
#import "SSZipArchive.h"
#import "PCH.h"
@interface DownloadFileManager ()<SSZipArchiveDelegate>
@end
@implementation DownloadFileManager
-(void)download
{
    //1.创建会话管理者
    AFHTTPSessionManager *manager =[AFHTTPSessionManager manager];
    
    NSURL *url = [NSURL URLWithString:@"http://192.168.2.189:8080/DSFA/news/apptest.zip"];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    //2.下载文件
    /*
     第一个参数:请求对象
     第二个参数:progress 进度回调 downloadProgress
     第三个参数:destination 回调(目标位置)
     有返回值
     targetPath:临时文件路径
     response:响应头信息
     第四个参数:completionHandler 下载完成之后的回调
     filePath:最终的文件路径
     */
    
    /*
     第一个参数:请求对象
     第二个参数:进度回调
     downloadProgress.completedUnitCount :已经下载的数据
     downloadProgress.totalUnitCount:数据的总大小
     第三个参数:destination回调,该block需要返回值(NSURL类型),告诉系统应该把文件剪切到什么地方
     targetPath:文件的临时保存路径tmp,随时可能被删除
     response:响应头信息
     第四个参数:completionHandler请求完成后回调
     response:响应头信息
     filePath:文件的保存路径,即destination回调的返回值
     error:错误信息
     */
    NSURLSessionDownloadTask *download = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
        
        //监听下载进度
        //completedUnitCount 已经下载的数据大小
        //totalUnitCount     文件数据的中大小
        NSLog(@"%f",1.0 *downloadProgress.completedUnitCount / downloadProgress.totalUnitCount);
        
    } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
        /**
         * 1:1:请求路径:NSUrl *url = [NSUrl urlWithString:path];从网络请求路径  2:把本地的file文件路径转成url,NSUrl *url = [NSURL fileURLWithPath:fullPath];
         2:返回值是一个下载文件的路径
         *
         */
        NSString *fullPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:response.suggestedFilename];
        
        NSLog(@"targetPath:%@",targetPath);
        NSLog(@"fullPath:%@",fullPath);
        
        return [NSURL fileURLWithPath:fullPath];
    } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
        /**
         *filePath:下载后文件的保存路径
         */
        NSLog(@"%@",filePath);
        NSString *imgFilePath = [filePath path];// 将NSURL转成NSString
       
        NSString *path =  NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
        [self releaseZipFilesWithUnzipFileAtPath:imgFilePath Destination:path];
    }];
    
    
    //3.执行Task
    [download resume];
}
// 解压
- (void)releaseZipFilesWithUnzipFileAtPath:(NSString *)zipPath Destination:(NSString *)unzipPath{
    NSError *error;
    if ([SSZipArchive unzipFileAtPath:zipPath toDestination:unzipPath overwrite:YES password:nil error:&error delegate:self]) {
        NSLog(@"success");
        NSLog(@"unzipPath = %@",unzipPath);
    }else {
        NSLog(@"%@",error);
    }
}
#pragma mark - SSZipArchiveDelegate
- (void)zipArchiveWillUnzipArchiveAtPath:(NSString *)path zipInfo:(unz_global_info)zipInfo {
    NSLog(@"将要解压。");
}
- (void)zipArchiveDidUnzipArchiveAtPath:(NSString *)path zipInfo:(unz_global_info)zipInfo unzippedPath:(NSString *)unzippedPath{
    NSLog(@"解压完成!");
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,259评论 4 61
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,679评论 25 708
  • 日内瓦在周末时就是一座空城,商店关门,人去楼空,我如幽灵般游走在大街上。这里的照片和说法是加入了夸张的元素,但的确...
    肖庆in上海阅读 204评论 0 1
  • 函数是重用的程序段。封装某块语句并赋予一个名称,然后可在程序的任何地方使用这个名称任意多次地运行该代码块。pyth...
    海阔天空呢阅读 180评论 0 0
  • 篝火,篝火,笑闹泸沽客过。桥头少女婀娜,青稞淡酒洒脱。谁错,谁错,双鹤穿云渐没。
    小木山庄的溜溜阅读 197评论 4 5