iOS下載zip 解壓 加載其中的html(附帶搭建簡易本地測試服務器)

1、有遠程zip下載鏈接的可以跳過這一步(搭建本地簡易服務器)

簡書鏈接:http://www.lxweimin.com/p/ad0833f4845a

2、 做前期準備? 導入需要的庫文件??

? ?1) AFNetworking 和SSZipArchive(一個加載庫 一個解壓庫 )

podfile:

platform :ios,'8.0'

target 'your? project name' do

pod 'AFNetworking'

pod 'SSZipArchive'

end

沒有pods 的看這里 cocoapods安裝以及使用?http://www.lxweimin.com/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{

? ? //遠程地址

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

? ? //默認配置

? ? NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];

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

? ? //請求

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


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


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

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

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

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

? ? ? ? //- block的返回值, 要求返回一個URL, 返回的這個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) {

? ? ? ? //設置下載完成操作


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

? ? ? ? NSString*htmlFilePath = [filePathpath];// 將NSURL轉成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;

? ? // 目標路徑,

? ? NSString*destinationPath = unzipPath;

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

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


//? ? ------------ 帶回調的解壓? ? ------------

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

? ? ? ? // entry : 解壓出來的文件名

? ? ? ? //entryNumber : 第幾個, 從1開始

? ? ? ? //total : 總共幾個

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

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

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

? ? ? ? //succeeded 是否成功

? ? ? ? // error 錯誤信息

? ? ? ? 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中的網頁

//? ? /初始化一個WKWebViewConfiguration對象

? ? WKWebViewConfiguration *config = [WKWebViewConfiguration new];

? ? //初始化偏好設置屬性:preferences

? ? config.preferences = [WKPreferences new];

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

? ? config.preferences.minimumFontSize = 10;

? ? //是否支持JavaScript

? ? config.preferences.javaScriptEnabled = YES;

? ? //不通過用戶交互,是否可以打開窗口

? ? 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? 但是用以上方法可以 應該是后面的?fileURLWithPath起作用了

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


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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容