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”目录中.(联系人是一个数组集合,内部为自定义对象). 功能如下: ...
随机推荐
- chrome 修改标签页
插件名称:New Tab Redirect 标签格式:"file:///home/user/index.html"
- java 倒序输出
输入五句话,用数组存储起来,然后逆序输出这五句话. import java.util.Scanner; class work1{ public static void main(String[] a ...
- Start:at cnblogs firstDay
C#旨在设计成为一种"简单.现代.通用",以及面向对象的程序设计语言,此种语言的实现,应提供对于以下软件工程要素的支持:强类型检查.数组维度检查.未初始化的变量引用检测.自动垃圾收 ...
- JavaScript数据结构,队列和栈
在JavaScript中为数组封装了大量的方法,比如:concat,pop,push,unshift,shift,forEach等,下面我将使用JavaScript提供的这些方法,实现队列和栈的操作. ...
- tornado框架之路二
二.路由系统 路由系统其实就是 url 和 类 的对应关系,这里不同于其他框架,其他很多框架均是 url 对应 函数,Tornado中每个url对应的是一个类. #!/usr/bin/env pyth ...
- 恶心的Oracle的if else if...
出处:http://blog.sina.com.cn/s/blog_407d47e60100d8ig.html 前段时间写Oracle存储过程就遇到问题.原来写成这样if 1=2 then null ...
- MSP430F149学习之路——AD
代码一:Timer_A触发转换 #include <msp430x14x.h> void main() { WDTCTL = WDTPW + WDTHOLD; P6SEL |= BIT0; ...
- No.002 Add Two Numbers
Add Two Numbers Total Accepted: 160702 Total Submissions: 664770 Difficulty: Medium You are given tw ...
- c#中如何做日期的三元判断(日期不为空赋值)
<dx:ASPxDateEdit runat="server" ID="edTab4_protocoldate" Width="100%&quo ...
- 隐藏vbs执行cmd命令的窗口
dim obj_shellset obj_shell = createobject("wscript.shell") host = WScript.FullNameIf LCase ...