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:@"文件没下载下来"];
//
//    }

}

///下载文件

+(void)downloadH5FileWithUrl:(NSDictionary*)data downloadProgress:(void (^)(NSProgress * _Nonnull downloadProgress))Progress success:(successBlock)result failed:(failedBlock)failed{
    //构造资源链接
    NSString *urlString = data[@"downloadPath"];

    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    //创建AFN的manager对象
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:configuration];
    //构造URL对象
    NSURL *url = [NSURL URLWithString:urlString];
    //构造request对象
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    //使用系统类创建downLoad Task对象
   NSURLSessionDownloadTask *task = [manager downloadTaskWithRequest:request progress:^(NSProgress *downloadProgress) {
       // 下载进度
//        self.progressView.progress = 1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount;
//        self.progressLabel.text = [NSString stringWithFormat:@"当前下载进度:%.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);
           }
   }];

    //开始请求
    [task resume];
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容