思路是

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

2.解压zip到程序目录

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

一、下载zip资源

[cpp]
-(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; 
}

-(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包

[cpp]
- (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]; 
     
}

- (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.调去函数

[cpp]
- (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"]; 
}

- (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"];
}

IOS下载资源zip到本地然后读取的更多相关文章

  1. ios htttp网络请求cookie的读取与写入(NSHTTPCookieStorage)

    当你访问一个网站时,NSURLRequest都会帮你主动记录下来你访问的站点设置的Cookie,如果 Cookie 存在的话,会把这些信息放在 NSHTTPCookieStorage 容器中共享,当你 ...

  2. iOS应用架构谈 本地持久化方案及动态部署

    转载: iOS应用架构谈 本地持久化方案及动态部署 前言 嗯,你们要的大招.跟着这篇文章一起也发布了CTPersistance和CTJSBridge这两个库,希望大家在实际使用的时候如果遇到问题,就给 ...

  3. IOS开发-加载本地音乐

    IOS开发-加载本地音乐 $(function () { $('pre.prettyprint code').each(function () { var lines = $(this).text() ...

  4. iOS WebView 加载本地资源(图片,文件等)

    https://www.cnblogs.com/dhui69/p/5596917.html iOS WebView 加载本地资源(图片,文件等) NSString *path = [[NSBundle ...

  5. iOS开发中UILocalNotification本地通知实现简单的提醒功能

    这段时间项目要求做一个类似的闹钟提醒功能,对通知不太熟悉的我,决定先用到xcode自带的本地通知试试,最终成功的实现了功能,特整理分享下. 它的表现特点: app关闭的时候也能接收和显示通知. app ...

  6. iOS在线更新framework,使用NSBundle动态读取

    官方文档:https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/LoadingCode/Tasks/Loadin ...

  7. iOS 疑难杂症 — — 推送本地国际化 loc-key 本地化失败的问题

    声明  欢迎转载,但请保留文章原始出处:)  博客园:http://www.cnblogs.com 农民伯伯: http://over140.cnblogs.com 正文 一.准备 推送本地国际化官方 ...

  8. ios推送:本地通知UILocalNotification

    Notification是智能手机应用编程中非常常用的一种传递信息的机制,而且可以非常好的节省资源,不用消耗资源来不停地检查信息状态(Pooling),在iOS下应用分为两种不同的Notificati ...

  9. iOS开发——数据持久化&本地数据的存储(使用NSCoder将对象保存到.plist文件)

    本地数据的存储(使用NSCoder将对象保存到.plist文件)   下面通过一个例子将联系人数据保存到沙盒的“documents”目录中.(联系人是一个数组集合,内部为自定义对象).   功能如下: ...

随机推荐

  1. UIPickView 和 UIDatePicker

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  2. 利用sql批量删除表,存储过程

    利用sql批量删除表,存储过程. 最近用godaddy的空间,由于系统里面的表多,一个个的删除很麻烦,就网上搜集了一下解决方法. 给大家分享一下: 1.批量删除存储过程 declare @procNa ...

  3. android 解决ScrollView嵌套ListView的问题,不能全屏,全屏不能显示下面控件

    在开发中遇到ScrollView嵌套ListView的问题,最开始发出不能全屏,效果是这样的: 但我想要的效果是这样的: 下面看一下布局文件: <?xml version="1.0&q ...

  4. 查询数据库中表或视图或存储过程的数量 sql 语句

    如果一个数据库中表的数量较多的话,那么在统计数据库中表的数量或视图或存储过程的数量时,如果还有一个一个去数,那就太麻烦了,其实可以通过 sql 语句来查询的,sql 语句的查询方法如下: sql se ...

  5. 使用POI导入Excel异常Cannot get a text value from a numeric cell 解决

    POI操作Excel时因为Excel数据Cell有不同的类型,会出现Cannot get a text value from a numeric cell的异常错误. 异常原因:Excel数据Cell ...

  6. ASPxTreeList控件去根节点的新增修改操作(写在onCommandColumnButtonInitialize()事件中)

    treelist去掉根节点按钮效果图: //去掉父节点及子节点旁的新增.修改.删除操作(写在onCommandColumnButtonInitialize事件中) protected void Tre ...

  7. 【spring 4】AOP:动态代理

    一.动态代理简介 动态代理与普通代理相比较,最大的好处是接口中声明的所有方法都被转移到一个集中的方法中处理(invoke),这样,在接口方法数量比较多的时候,我们可以进行灵活处理,而不需要像静态代理那 ...

  8. hql语句关联查询(select new )

    在  new bean()中,对象里要有其构造方法,查询参数必须是构造方法中的参数,get set也要同步 类似于 String hql= "select  new A(a.id, a.na ...

  9. netlink机制

    一.netlink机制简介 netlink是一种基于网络的机制,允许在内核内部以及内核与用户之间进行通信.正式定义见RFC3549.手册见netlink(3)和netlink(7).netlink(3 ...

  10. .NET Web开发总结

    在aspx文件中  创建控件 在右下角有控件信息 按类排序 会将控件信息安装类排序 点击控件 会增加属性页面的分页[事件]页面  可以增加其事件函数 字符串操作及其时间操作 fn_name.Inser ...