//
// ViewController.m
// AFN断点续传演练
//
// Created by apple on 13-7-30.
// Copyright (c) 2013年 Jackie. All rights reserved.
//
#import "ViewController.h"
#import "AFNetworking.h"
#import "SSZipArchive.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - 下载文件
- (IBAction)downloadFiles:(id)sender
{
// 1. 指定下载文件地址
NSURL *url = [NSURL URLWithString:@"http://169.254.98.245/~apple/itcast/download/iTunesConnect_DeveloperGuide_CN.zip"];
// 2. 指定文件保存路径
NSArray *documents = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *downloadPath = [documents[0]stringByAppendingPathComponent:@"book.zip"];
// 3. 创建NSURLRequest
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 4. 创建AFURLConnectionOperation
AFURLConnectionOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];
// 5. 设置操作的输出流(在网络中的数据是以流的方式传输的,告诉操作把文件保存在第2步设置的路径中)
[operation setOutputStream:[NSOutputStream outputStreamToFileAtPath:downloadPath append:NO]];
// 6. 设置下载进程处理块代码
// 6.1 bytesRead 读取的字节——这一次下载的字节数
// 6.2 totalBytesRead 读取的总字节——已经下载完的
// 6.3 totalBytesExpectedToRead 希望读取的总字节——就是文件的总大小
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
// 做下载进度百分比的工作
NSLog(@"下载百分比:%f", (float)totalBytesRead / totalBytesExpectedToRead);
}];
// 7. 操作完成块代码
[operation setCompletionBlock:^{
// 解压缩的顺序
// 1. 定义要解压缩的文件 —— downloadPath
// 2. 要解压缩的目标目录
// 3. 调用类方法解压缩
[SSZipArchive unzipFileAtPath:downloadPath toDestination:documents[0]];
// 删除压缩包
[[NSFileManager defaultManager]removeItemAtPath:downloadPath error:nil];
}];
// 8 启动操作
[operation start];
}
@end