IOS下载资源zip到本地然后读取
思路是
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到本地然后读取的更多相关文章
- ios htttp网络请求cookie的读取与写入(NSHTTPCookieStorage)
当你访问一个网站时,NSURLRequest都会帮你主动记录下来你访问的站点设置的Cookie,如果 Cookie 存在的话,会把这些信息放在 NSHTTPCookieStorage 容器中共享,当你 ...
- iOS应用架构谈 本地持久化方案及动态部署
转载: iOS应用架构谈 本地持久化方案及动态部署 前言 嗯,你们要的大招.跟着这篇文章一起也发布了CTPersistance和CTJSBridge这两个库,希望大家在实际使用的时候如果遇到问题,就给 ...
- IOS开发-加载本地音乐
IOS开发-加载本地音乐 $(function () { $('pre.prettyprint code').each(function () { var lines = $(this).text() ...
- iOS WebView 加载本地资源(图片,文件等)
https://www.cnblogs.com/dhui69/p/5596917.html iOS WebView 加载本地资源(图片,文件等) NSString *path = [[NSBundle ...
- iOS开发中UILocalNotification本地通知实现简单的提醒功能
这段时间项目要求做一个类似的闹钟提醒功能,对通知不太熟悉的我,决定先用到xcode自带的本地通知试试,最终成功的实现了功能,特整理分享下. 它的表现特点: app关闭的时候也能接收和显示通知. app ...
- iOS在线更新framework,使用NSBundle动态读取
官方文档:https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/LoadingCode/Tasks/Loadin ...
- iOS 疑难杂症 — — 推送本地国际化 loc-key 本地化失败的问题
声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www.cnblogs.com 农民伯伯: http://over140.cnblogs.com 正文 一.准备 推送本地国际化官方 ...
- ios推送:本地通知UILocalNotification
Notification是智能手机应用编程中非常常用的一种传递信息的机制,而且可以非常好的节省资源,不用消耗资源来不停地检查信息状态(Pooling),在iOS下应用分为两种不同的Notificati ...
- iOS开发——数据持久化&本地数据的存储(使用NSCoder将对象保存到.plist文件)
本地数据的存储(使用NSCoder将对象保存到.plist文件) 下面通过一个例子将联系人数据保存到沙盒的“documents”目录中.(联系人是一个数组集合,内部为自定义对象). 功能如下: ...
随机推荐
- Integer cache
View.findViewById采用深度遍历,找到第一个匹配的控件 Integer Cache public static void testIntegerCache() { Class cache ...
- Unity协程(Coroutine)原理深入剖析(转载)
记得去年6月份刚开始实习的时候,当时要我写网络层的结构,用到了协程,当时有点懵,完全不知道Unity协程的执行机制是怎么样的,只是知道函数的返回值是IEnumerator类型,函数中使用yield r ...
- 使用PetaPoco ORM 框架分页查询
通过在派生的Repository中调用GetPagingEntities方法来获取分页数据,并返回由PagingDataSet<T>封装分页集合,例如: Public PagingData ...
- 洛谷P2736 “破锣摇滚”乐队 Raucous Rockers
P2736 "破锣摇滚"乐队 Raucous Rockers 21通过 52提交 题目提供者该用户不存在 标签USACO 难度普及+/提高 提交 讨论 题解 最新讨论 暂时没有 ...
- java中HashMap的用法
重点介绍HashMap.首先介绍一下什么是Map.在数组中我们是通过数组下标来对其内容索引的,而在Map中我们通过对象来对对象进行索引,用来索引的对象叫做key,其对应的对象叫做value.在下文中会 ...
- CSS3之选择器
总结了下CSS3新增的一些选择器. CSS3的选择器有基本选择器.属性选择器.伪类选择器几类. CSS3选择器 选择器 举例 例子描述 element1~element2 p~a 选择前面有 < ...
- PC上安装MAC X Lion
PC上安装MACXLion 网上关于如何在PC下安装MAC的文章已近不少了,但对于一些初学者在实践当中会遇到各种问题,以下视频资料为大家展示两种虚拟机安装MacOS. 1.VmwareWorkstat ...
- JSF 监听
JSF项目中实现基于RBAC模型的权限管理设计(二) 转 4.3 权限验证模块设计 一个好的权限管理机制在项目中应用时,最好不要让程序员在具体业务代码的方法中来判断用户权限.因为这意味着大量重复的代码 ...
- Oracle多线程并行使用、关联与指定索引执行
nologging AS SELECT /*+parallel(4) leading(s a) use_hash(A) index(s IDX_CS_SERVICE_RECORD_MD2_04) */ ...
- Android IOS WebRTC 音视频开发总结(二二)-- 多人视频架构模式
本文主要介绍多人视频会议服务端架构方式,文章来自博客园RTC.Blacker,转载必须说明出处,欢迎关注个人微信公众号blacker,更多详见www.rtc.help 随着移动互联网的迅速发展,很多公 ...