美文网首页
iOS文件操作,解压文件,将资源文件copy到沙盒

iOS文件操作,解压文件,将资源文件copy到沙盒

作者: 樊二哈 | 来源:发表于2024-01-24 10:32 被阅读0次

//把本地的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];
}

相关文章

  • tidevice 文件操作命令

    1、拉取沙盒目录文件 比如: 2、推资源到沙盒目录 3、删除沙盒目录文件

  • 沙盒存储操作

    // 沙盒存储操作 /**每个iOS程序都有自己的文件系统目录,就是沙盒,并且与其他文件系统、其他应用程序隔离,并...

  • 沙盒

    一、iOS沙盒机制介绍(1)概念:每个ios应用都有自己的应用沙盒,应用沙盒就是文件系统目录,与其他应用放入文件 ...

  • iOS开发-隐私权限

    背景 iOS的安全机制——沙盒限制了应用程序执行各种操作的权限。沙盒实际就是程序的系统文件目录,非代码文件都在此保...

  • iOS之文件存储

    将字典存储到沙盒中(文件名后缀.plist) 从沙盒中读取字典数据 删除沙盒文件

  • iOS本地沙盒文件读取浏览下载,日志读取下载

    传统沙盒文件读取方式 谈到iOS本地沙盒文件读取下载,给人的初步印象是苹果对应用下面的沙盒文件管理严格,只能通过已...

  • iOS开发之文件操作

    iOS开发过程中,我们经常会遇到对文件的操作,如:获取沙盒路径、创建文件、修改文件、删除文件等。 所以现在对常用的...

  • IOS 从Resource文件夹下Copy文件到沙盒

  • iOS 文件操作

    iOS文件(沙盒目录,文件创建、移动、复制等) 目录获取获取沙盒的主目录路径: (NSString *)homeD...

  • iOS开发--沙盒存储

    沙盒简介 iOS中每个应用程序都有一个独立的文件夹,这个文件夹就是沙盒。沙盒用来存储app的本地文件,例如:音频、...

网友评论

      本文标题:iOS文件操作,解压文件,将资源文件copy到沙盒

      本文链接:https://www.haomeiwen.com/subject/xlzpodtx.html