iOS下載zip 解壓 加載其中的html(附帶搭建簡(jiǎn)易本地測(cè)試服務(wù)器)

1、有遠(yuǎn)程zip下載鏈接的可以跳過(guò)這一步(搭建本地簡(jiǎn)易服務(wù)器)

簡(jiǎn)書鏈接:http://www.itdecent.cn/p/ad0833f4845a

2、 做前期準(zhǔn)備? 導(dǎo)入需要的庫(kù)文件??

? ?1) AFNetworking 和SSZipArchive(一個(gè)加載庫(kù) 一個(gè)解壓庫(kù) )

podfile:

platform :ios,'8.0'

target 'your? project name' do

pod 'AFNetworking'

pod 'SSZipArchive'

end

沒(méi)有pods 的看這里 cocoapods安裝以及使用?http://www.itdecent.cn/p/6e71fbeb71b7

3、上代碼

1)@property(nonatomic,copy)NSString * urlstr;

@property(nonatomic,copy)NSString * decodestr;

@property(nonatomic,strong)WKWebView * webView;

下載zip代碼?

-(void)rquestZipArchivePath:(NSString*)pathUrl andHtmlVersion:(NSString*)version{

? ? //遠(yuǎn)程地址

? ? NSURL*URL = [NSURLURLWithString:pathUrl];

? ? //默認(rèn)配置

? ? NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];

? ? AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

? ? //請(qǐng)求

? ? NSURLRequest *request = [NSURLRequest requestWithURL:URL];


? ? NSURLSessionDownloadTask* downloadTask =[managerdownloadTaskWithRequest:requestprogress:^(NSProgress*_NonnulldownloadProgress) {


? ? ? ? doublecurr=(double)downloadProgress.completedUnitCount;

? ? ? ? doubletotal=(double)downloadProgress.totalUnitCount;

? ? ? ? NSLog(@"下載進(jìn)度==%.2f",curr/total);

? ? }destination:^NSURL*_Nonnull(NSURL*_NonnulltargetPath,NSURLResponse*_Nonnullresponse) {

? ? ? ? //- block的返回值, 要求返回一個(gè)URL, 返回的這個(gè)URL就是文件的位置的路徑


? ? ? ? NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];


? ? ? ? //再次之前先刪除本地文件夾里面相同的文件夾

? ? ? ? NSFileManager*fileManager = [NSFileManagerdefaultManager];

? ? ? ? NSArray*contents = [fileManagercontentsOfDirectoryAtPath:cachesPatherror:NULL];

? ? ? ? NSEnumerator*e = [contentsobjectEnumerator];

? ? ? ? NSString*filename;

? ? ? ? NSString*extension =@"zip";

? ? ? ? while((filename = [enextObject])) {


? ? ? ? ? ? if([[filenamepathExtension]isEqualToString:extension]) {


? ? ? ? ? ? ? ? [fileManagerremoveItemAtPath:[cachesPathstringByAppendingPathComponent:filename]error:NULL];

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? NSString*path = [cachesPathstringByAppendingPathComponent:response.suggestedFilename];

? ? ? ? return[NSURLfileURLWithPath:path];

? ? }completionHandler:^(NSURLResponse*_Nonnullresponse,NSURL*_NullablefilePath,NSError*_Nullableerror) {

? ? ? ? //設(shè)置下載完成操作


? ? ? ? // filePath就是你下載文件的位置,你可以解壓,也可以直接拿來(lái)使用

? ? ? ? NSString*htmlFilePath = [filePathpath];// 將NSURL轉(zhuǎn)成NSString

? ? ? ? NSArray*documentArray =? NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);

? ? ? ? NSString *path = [[documentArray lastObject] stringByAppendingPathComponent:@"Preferences"];


? ? ? ? NSFileManager*fileManager = [NSFileManagerdefaultManager];

? ? ? ? [fileManagerremoveItemAtPath:[NSString stringWithFormat:@"%@/html",path] error:nil];

? ? ? ? self.urlstr=htmlFilePath;

? ? ? ? self.decodestr=path;

? ? }];

? ? [downloadTaskresume];

}

解壓代碼

- (void)releaseZipFilesWithUnzipFileAtPath:(NSString*)zipPath Destination:(NSString*)unzipPath{

? ? //? ? NSLog(@"%@,%@",zipPath,unzipPath);

? ? NSError*error;

? ? if ([SSZipArchive unzipFileAtPath:zipPath toDestination:unzipPath overwrite:YES password:nil error:&error delegate:self]) {

? ? ? ? NSLog(@"success");

? ? }

? ? else{

? ? ? ? NSLog(@"%@",error);

? ? }

? // 壓縮包的全路徑(包括文件名)

//? ? NSString *destinationPath = zipPath;

? ? // 目標(biāo)路徑,

? ? NSString*destinationPath = unzipPath;

? ? // 解壓, 返回值代表是否解壓完成

? ? Booleanb= [SSZipArchiveunzipFileAtPath:zipPathtoDestination:destinationPath];


//? ? ------------ 帶回調(diào)的解壓? ? ------------

? ? Booleanb1 = [SSZipArchiveunzipFileAtPath:zipPathtoDestination:destinationPathprogressHandler:^(NSString*_Nonnullentry,unz_file_infozipInfo,longentryNumber,longtotal) {

? ? ? ? // entry : 解壓出來(lái)的文件名

? ? ? ? //entryNumber : 第幾個(gè), 從1開(kāi)始

? ? ? ? //total : 總共幾個(gè)

? ? ? ? NSLog(@"progressHandler:%@, entryNumber:%ld, total:%ld? names:%@", entry, entryNumber, total,destinationPath);

? ? }completionHandler:^(NSString*_Nonnullpath,BOOLsucceeded,NSError*_Nullableerror) {

? ? ? ? //path : 被解壓的壓縮吧全路徑

? ? ? ? //succeeded 是否成功

? ? ? ? // error 錯(cuò)誤信息

? ? ? ? NSLog(@"completionHandler:%@, , succeeded:%d, error:%@", path, succeeded, error);

? ? }];

}

#pragma mark - SSZipArchiveDelegate

- (void)zipArchiveWillUnzipArchiveAtPath:(NSString*)path zipInfo:(unz_global_info)zipInfo {

? ? NSLog(@"將要解壓%d",zipInfo.number_entry);


}

- (void)zipArchiveDidUnzipArchiveAtPath:(NSString*)path zipInfo:(unz_global_info)zipInfo unzippedPath:(NSString*)unzippedPat uniqueId:(NSString*)uniqueId {

? ? NSLog(@"解壓完成!");

}

展示加載zip中的網(wǎng)頁(yè)

//? ? /初始化一個(gè)WKWebViewConfiguration對(duì)象

? ? WKWebViewConfiguration *config = [WKWebViewConfiguration new];

? ? //初始化偏好設(shè)置屬性:preferences

? ? config.preferences = [WKPreferences new];

? ? //The minimum font size in points default is 0;

? ? config.preferences.minimumFontSize = 10;

? ? //是否支持JavaScript

? ? config.preferences.javaScriptEnabled = YES;

? ? //不通過(guò)用戶交互,是否可以打開(kāi)窗口

? ? config.preferences.javaScriptCanOpenWindowsAutomatically = YES;


? ? self.webView=[[WKWebView alloc]initWithFrame:self.view.bounds configuration:config];

? ? _webView.navigationDelegate=self;

? ? _webView.UIDelegate = self;


//

? ? NSArray *documentArray = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);

? ? NSString *path = [[documentArray lastObject] stringByAppendingPathComponent:@"Preferences"];

? ? NSURL *url=[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/dist/index.html",path]];

? ? NSString*urlStr = [urlabsoluteString];

? ? urlStr = [urlStrstringByReplacingOccurrencesOfString:@"file://" withString:@""];

? ?[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:urlStr]]];

? ? //uiwebview和wkwebview加載方式略有不同 wkwebview用loadrequest方法不能加載本地html? 但是用以上方法可以 應(yīng)該是后面的?fileURLWithPath起作用了

? ? [self.viewaddSubview:self.webView];


附demo地址??https://github.com/fc931127/xaizaizip.git

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容