iOS文件操作,解壓文件,將資源文件copy到沙盒

//把本地的html資源文件拷貝到沙盒

        NSString * docPath = [[NSBundle mainBundle] pathForResource:@"HTML" ofType:@"zip"];
            // 沙盒Documents目錄
        NSString * appDir = [NSString stringWithFormat:@"%@/Documents/%@",NSHomeDirectory(),@""];

        BOOL filesPresent = [NSFileControls copyMissingFile:docPath toPath:appDir];

解壓文件

+ (void)unarchiveButtonAction:(NSString*)filePath pass:(NSString*)password filename:(NSString*)filename version:(NSString*)version{
//    NSFileManager * fileManager = [NSFileManager defaultManager];
    
    NSString * htmlFilePath = [NSString stringWithFormat:@"%@/Documents/%@",NSHomeDirectory(),filename];
    NSString *savePath = [NSString stringWithFormat:@"%@/Documents/%@",NSHomeDirectory(),@"HTML"];
//    if ([fileManager fileExistsAtPath:filePath]) {
        [SSZipArchive unzipFileAtPath:htmlFilePath toDestination:savePath overwrite:YES password:password progressHandler:^(NSString * _Nonnull entry, unz_file_info zipInfo, long entryNumber, long total) {
            NSString * progressValue = [NSString stringWithFormat:@"%.2f",entryNumber*1.0/total];
            NSLog(@"%@,%@",progressValue,entry);
            dispatch_async(dispatch_get_main_queue(), ^{
                DLog(@"progressValue");
            });
            //                /Users/liaoshen/Library/Developer/CoreSimulator/Devices/93E53B77-DEDF-4261-AE6A-FB402A919B02/data/Containers/Data/Application/8F5D26CA-1248-44FF-9430-EAA61D0D0716/Documents/csV1.0.1.zip
        } completionHandler:^(NSString * _Nonnull path, BOOL succeeded, NSError * _Nullable error) {
            
            NSLog(@"%@,%d,%@",path,succeeded,error);
            if(!succeeded) {
                [NSFileControls insertFileErrMsg:@"unzip" msg:[NSString stringWithFormat:@"%@:%@",@"解壓失敗",error]];
            } else {
                if(password.length == 0) {
                    [NSFileControls getHtmlVersion];
                } else {
                    [kUserDefaults setObject:version forKey:@"HtmlVersion"];
                    [kUserDefaults synchronize];
                }
                dispatch_async(dispatch_get_main_queue(), ^{
                    DLog(@"解壓完成");
                });
            }
        }];
//    } else {
//        [NSFileControls insertFileErrMsg:@"copy" msg:@"文件沒(méi)下載下來(lái)"];
//
//    }

}

///下載文件

+(void)downloadH5FileWithUrl:(NSDictionary*)data downloadProgress:(void (^)(NSProgress * _Nonnull downloadProgress))Progress success:(successBlock)result failed:(failedBlock)failed{
    //構(gòu)造資源鏈接
    NSString *urlString = data[@"downloadPath"];

    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    //創(chuàng)建AFN的manager對(duì)象
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:configuration];
    //構(gòu)造URL對(duì)象
    NSURL *url = [NSURL URLWithString:urlString];
    //構(gòu)造request對(duì)象
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    //使用系統(tǒng)類(lèi)創(chuàng)建downLoad Task對(duì)象
   NSURLSessionDownloadTask *task = [manager downloadTaskWithRequest:request progress:^(NSProgress *downloadProgress) {
       // 下載進(jìn)度
//        self.progressView.progress = 1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount;
//        self.progressLabel.text = [NSString stringWithFormat:@"當(dāng)前下載進(jìn)度:%.2f%%",100.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount];
           
   } destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
           
       NSURL *path = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
       return [path URLByAppendingPathComponent:data[@"filename"]];
         
       } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
       NSLog(@"File downloaded to: %@", filePath);
           if (!error) {
               result(@{@"data":response,@"url":filePath.absoluteString});
           } else {
               failed (error);
           }
   }];

    //開(kāi)始請(qǐng)求
    [task resume];
}
?著作權(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)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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