//
// JYHRepaymentController.m
// JYH_iOS
//
// Created by Angela on 2017/8/28.
// Copyright © 2017年 Angela. All rights reserved.
//工具链接 https://github.com/ZipArchive/ZipArchive
#import "JYHFindController.h"
#import "ZipArchive.h"
@interface JYHFindController ()<SSZipArchiveDelegate>
@property (nonatomic, strong) UIWebView *findWebView;
@property (nonatomic, copy) NSString *orderHtmlStr;
@end
@implementation JYHFindController
- (void)viewDidLoad {
[super viewDidLoad];
[self findwebUI];
}
-(void)findwebUI{
self.findWebView=[[UIWebView alloc]initWithFrame:self.view.bounds];
JYHBaseWebController *findWebVC=[[JYHBaseWebController alloc]init];
[self.view addSubview:findWebVC.view];
[findWebVC.view addSubview:self.findWebView];
[self downFileFromServer];
self.orderHtmlStr=@"creditCard";
NSArray *documentArray = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *path = [[documentArray lastObject] stringByAppendingPathComponent:@"Preferences"];
NSURL *url=[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@/index.html",path,self.orderHtmlStr]];
NSString *urlStr = [url absoluteString];
urlStr = [urlStr stringByReplacingOccurrencesOfString:@"file://" withString:@""];
NSURL * URL = [NSURL URLWithString:urlStr];
NSURLRequest *request=[NSURLRequest requestWithURL:URL];
[self.findWebView loadRequest:request];
}
- (void)downFileFromServer{
//远程地址
NSURL *URL = [NSURL URLWithString:@"http://zip.yourcard.com.cn/zip/creditCard.zip"];
//默认配置
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
//AFN3.0+基于封住URLSession的句柄
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
//请求
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
//下载Task操作
NSURLSessionDownloadTask * downloadTask= [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
// 下载进度
} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
//- block的返回值, 要求返回一个URL, 返回的这个URL就是文件的位置的路径
NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *contents = [fileManager contentsOfDirectoryAtPath:cachesPath error:NULL];
NSEnumerator *e = [contents objectEnumerator];
NSString *filename;
NSString *extension = @"zip";
while ((filename = [e nextObject])) {
if ([[filename pathExtension] isEqualToString:extension]) {
[fileManager removeItemAtPath:[cachesPath stringByAppendingPathComponent:filename] error:NULL];
}
}
NSString *path = [cachesPath stringByAppendingPathComponent:response.suggestedFilename];
return [NSURL fileURLWithPath:path];
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
//设置下载完成操作
// filePath就是你下载文件的位置,你可以解压,也可以直接拿来使用
NSString *htmlFilePath = [filePath path];// 将NSURL转成NSString
NSArray *documentArray = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *path = [[documentArray lastObject] stringByAppendingPathComponent:@"Preferences"];
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:[NSString stringWithFormat:@"%@",path] error:nil];
[self releaseZipFilesWithUnzipFileAtPath:htmlFilePath Destination:path];
}];
[downloadTask 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 *)unzippedPat uniqueId:(NSString *)uniqueId {
NSLog(@"解压完成!");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end