(一二九)获取文件的MineType、利用SSZipArchive进行压缩解压
2015-09-25 16:54
405 查看
MineType
简介
文件在网络上以二进制流的方式传播,为了区分不同的文件类型,用MineType来标明。为什么要获取
文件的拓展名较短,比较好记,但是MineType是很长的,比如docx拓展名的MineType是
application/vnd.openxmlformats-officedocument.wordprocessingml.document,因此比较合适的方案是根据拓展名直接得到MineType。
怎么做
比较幸运,通过URLConnection的响应体response就能拿到MineType,我们只需要把获取本地文件包装成一个URL请求,然后拿到response即可,代码如下:NSURL *url = [[NSBundle mainBundle] URLForResource:@"test" withExtension:@"docx"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { NSLog(@"%@",response.MIMEType); }];
文件的压缩与解压
第三方框架
SSZipArchive是一个用于ZIP压缩与解压的第三方框架,可作为工具类使用,十分方便。导入
下载完毕后,将minizip文件夹与SSZipArchive类文件导入工程,并且添加动态库libz.dylib(在Build Phases的Link Binary With Libraries中添加)。使用
通过类方法createZipFileAtPath:withFilesAtPaths:和unzipFileAtPath:toDestination:分别可以实现压缩和解压。假设现在工程中有4_7inch1.jpg~4_7inch4.jpg四个文件,下面的代码在触摸开始时对他们进行压缩,并保存在沙盒的Library/Caches中;在触摸结束时将其解压。注意解压时应该指定一个目录,否则会在当前目录解压所有文件。
通过类方法的返回值可以判断操作是否成功。
代码如下:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ NSMutableArray *paths = [NSMutableArray array]; for (int i = 1; i <= 4; i++) { NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"4_7inch%d.jpg",i] ofType:nil]; [paths addObject:path]; } NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; NSString *outPath = [cachePath stringByAppendingPathComponent:@"imgs.zip"]; if([SSZipArchive createZipFileAtPath:outPath withFilesAtPaths:paths]){ NSLog(@"success"); } } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; NSString *zipPath = [cachePath stringByAppendingPathComponent:@"imgs.zip"]; if ([SSZipArchive unzipFileAtPath:zipPath toDestination:[cachePath stringByAppendingPathComponent:@"imgs/"]]) { NSLog(@"success"); } }
相关文章推荐
- Android-ThreadPool
- SQLServer2008/2005 生成数据字典SQL语句
- 【Jenkins系列之六】Jenkins分布式构建基础——Master/Slave架构
- redis cluster实现
- 选择器
- Java基础高效复习(hashmap的实现原理)
- (一二九)获取文件的MineType、利用SSZipArchive进行压缩解压
- Sahi访问API基础知识(上)
- SVN 常识
- JavaScript进阶内容1:各种对象类型检测
- 动态链接库中函数的地址确定---PLT和GOT [转]
- vs2013类模板的修改
- Mac 下安装及配置 ProxyChains-NG 实现终端下代理
- Length of Last Word
- 发邮件
- lua中的pairs和ipairs
- 响应式设计种种……
- bat批处理完成jdk tomcat的安装
- 错误 1 error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
- ADT和Android SDK的安装