IOS下载资源zip到本地然后读取

举报
清雨小竹 发表于 2022/09/25 00:53:02 2022/09/25
1.8k+ 0 0
【摘要】 欢迎访问我的接口小站:www.chinadev.cn 思路是 1.ios下载服务器上的zip资源包(图片,声音等经过zip压缩的资源包)到本地 2.解压zip到程序目录 3.从程序目录加载资源文件 一、下载zip资源 -(NSString*)DownloadTextFile:(NSString*)fileUrl ...

欢迎访问我的接口小站:www.chinadev.cn

思路是

1.ios下载服务器上的zip资源包(图片,声音等经过zip压缩的资源包)到本地

2.解压zip到程序目录

3.从程序目录加载资源文件


一、下载zip资源


      -(NSString*)DownloadTextFile:(NSString*)fileUrl   fileName:(NSString*)_fileName
      {
          NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask,YES);//使用C函数NSSearchPathForDirectoriesInDomains来获得沙盒中目录的全路径。
          NSString *ourDocumentPath =[documentPaths objectAtIndex:0];
          NSString *sandboxPath = NSHomeDirectory();
          NSString *documentPath = [sandboxPath  stringByAppendingPathComponent:@"TestDownImgZip.app"];//将Documents添加到sandbox路径上//TestDownImgZip.app
          NSString *FileName=[documentPath stringByAppendingPathComponent:_fileName];//fileName就是保存文件的文件名
          NSFileManager *fileManager = [NSFileManager defaultManager];
         // Copy the database sql file from the resourcepath to the documentpath
         if ([fileManager fileExistsAtPath:FileName])
          {
             return FileName;
          }else
          {
              NSURL *url = [NSURL URLWithString:fileUrl];
              NSData *data = [NSData dataWithContentsOfURL:url];
              [data writeToFile:FileName atomically:YES];//将NSData类型对象data写入文件,文件名为FileName
          }
         return FileName;
      }
  
 



2.解压zip包


      - (void)OpenZip:(NSString*)zipPath  unzipto:(NSString*)_unzipto
      {
          ZipArchive* zip = [[ZipArchive alloc] init];
         if( [zip UnzipOpenFile:zipPath] )
          {
              BOOL ret = [zip UnzipFileTo:_unzipto overWrite:YES];
             if( NO==ret )
              {
                 NSLog(@"error");
              }
              [zip UnzipCloseFile];
          }
          [zip release];
      }
  
 


3.调去函数


      - (IBAction)ShowImg:(id)sender {
          NSString *filePath = [self DownloadTextFile:@"http://www.xtox.net/img.zip" fileName:@"img.zip"];
         NSLog(filePath);
          NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask,YES);//使用C函数NSSearchPathForDirectoriesInDomains来获得沙盒中目录的全路径。
          NSString *ourDocumentPath =[documentPaths objectAtIndex:0];
          NSString *sandboxPath = NSHomeDirectory();
          NSString *documentPath = [sandboxPath  stringByAppendingPathComponent:@"TestDownImgZip.app"];//将Documents添加到sandbox路径上//TestDownImgZip.app
          [self OpenZip:filePath unzipto:documentPath];
          self.imgView.image = [UIImage imageNamed:@"img/1.png"];
      }
  
 


文章来源: zzzili.blog.csdn.net,作者:清雨小竹,版权归原作者所有,如需转载,请联系作者。

原文链接:zzzili.blog.csdn.net/article/details/9029025

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

作者其他文章

评论(0

抱歉,系统识别当前为高风险访问,暂不支持该操作

    全部回复

    上滑加载中

    设置昵称

    在此一键设置昵称,即可参与社区互动!

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。