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”目录中.(联系人是一个数组集合,内部为自定义对象). 功能如下: ...
随机推荐
- that-annoying-insert-problem-getting-data-into-the-db-using-dapper
http://samsaffron.com/archive/2012/01/16/that-annoying-insert-problem-getting-data-into-the-db-using ...
- Android设置不被系统设置改变的字体大小
原因 从4.0开始,系统设置中“显示”可以对字体大小进行配置,这会影响到TextView等控件中文字显示的大小. 解决方案 在自定义的Activity中重写getResources方法 @Overri ...
- 《Head First 设计模式》ch.3 装饰(Decorator)模式
设计原则 类应该对修改关闭,对扩展开放(开放-关闭原则).在每个地方使用开放-关闭原则是一种浪费,也没有必要,因为这通常会引入新的抽象层次,增加代码复杂度.需要把注意力集中在设计中最有可能改变的地方. ...
- android下拉菜单 spinner 学习
首先看一下继承关系: public class Spinner extends AbsSpinner implements DialogInterface.OnClickListener Class ...
- C语言-删除重复字符
//实现对一个字符串的删除重复的字符,并输出删除后的字符串,(如从”testroadeasy”中输出”tesroady”) Action(){ char *str="testroade ...
- 我的Python基础笔记
Python是从刚开始参加工作,就有听各方面的测试大牛推崇,但是刚开始做测试时还是把基础的测试方法放在第一位来学习的,直到半年多以后才开始接触Python. 我的Python基础主要是以廖雪峰老师的在 ...
- note name
谦谦君子:借用<周易·谦>:“初六,谦谦君子,用涉大川,吉.” 温润如玉:化用<诗经·卫风·淇奥>“有匪君子,如切如磋,如琢如磨”之义.
- 【ITOO 2】使用ArrayList时的注意事项:去除多余的null值
问题描述:在课表导入的时候,将数据从excel表里读出,然后将list批量插入到对应的课程表的数据表单中去,出现结果:当我们导入3条数据时,list.size()为3,但是实际上,list里面存在10 ...
- PAT1053. Path of Equal Weight
//之前一直尝试用vector存储path,但是每次错误后回退上一级节点时不能争取回退,导致探索路径正确,但是输出不正确,用参数num,标记前一个路径点的位置传递参数,就好多了 //其中在输入时就将后 ...
- 2016-03-10:libx265源码解析
单步跟踪执行流程 将cli设定为启动项目,在属性->调试->命令行参数中设置如下参数: --input E:\video\pedestrian_area.yuv --fps 24 --in ...