ios开发网络学习五:MiMEType ,多线程下载文件思路,文件的压缩和解压缩
一:MiMEType:一般可以再百度上搜索到相应文件的MiMEType,或是利用c语言的api去获取文件的MiMEType


;
//对该文件发送一个异步请求,拿到文件的MIMEType
- (void)MIMEType
{
// NSString *file = @"file:///Users/文顶顶/Desktop/test.png";
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:@"/Users/文顶顶/Desktop/test.png"]] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * __nullable response, NSData * __nullable data, NSError * __nullable connectionError) {
// response.MIMEType
NSLog(@"%@",response.MIMEType);
}];
}
```
(2)通过UTTypeCopyPreferredTagWithClass方法
```objc
//注意:需要依赖于框架MobileCoreServices
- (NSString *)mimeTypeForFileAtPath:(NSString *)path
{
if (![[[NSFileManager alloc] init] fileExistsAtPath:path]) {
return nil;
}
CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)[path pathExtension], NULL);
CFStringRef MIMEType = UTTypeCopyPreferredTagWithClass (UTI, kUTTagClassMIMEType);
CFRelease(UTI);
if (!MIMEType) {
return @"application/octet-stream";
}
return (__bridge NSString *)(MIMEType);
}
```
二:多线程文件下载思路:将下载文件分成若干段,每段任务创建一条线程,多个任务并发执行下载文件的操作,不能利用输出流进行文件二进制数据data的拼接,否则会出现数据错乱。利用文件句柄,在代理方法中,接受到数据后,判断是哪条线程,再根据线程的不同,将文件句柄的位置偏移到seekoffset,偏移到相应线程的下载的地方
三:文件的压缩和解压缩




#import "ViewController.h"
#import "SSZipArchive.h" @interface ViewController () @end @implementation ViewController
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self unzip];
} -(void)zip
{
NSArray *arrayM = @[
@"/Users/xiaomage/Desktop/Snip20160226_2.png",
@"/Users/xiaomage/Desktop/Snip20160226_6.png"
];
/*
第一个参数:压缩文件的存放位置
第二个参数:要压缩哪些文件(路径)
*/
//[SSZipArchive createZipFileAtPath:@"/Users/xiaomage/Desktop/Test.zip" withFilesAtPaths:arrayM];
[SSZipArchive createZipFileAtPath:@"/Users/xiaomage/Desktop/Test.zip" withFilesAtPaths:arrayM withPassword:@""];
} -(void)zip2
{
/*
第一个参数:压缩文件存放位置
第二个参数:要压缩的文件夹(目录)
*/
[SSZipArchive createZipFileAtPath:@"/Users/xiaomage/Desktop/demo.zip" withContentsOfDirectory:@"/Users/xiaomage/Desktop/demo"];
} -(void)unzip
{
/*
第一个参数:要解压的文件在哪里
第二个参数:文件应该解压到什么地方
*/
//[SSZipArchive unzipFileAtPath:@"/Users/xiaomage/Desktop/demo.zip" toDestination:@"/Users/xiaomage/Desktop/xx"]; [SSZipArchive unzipFileAtPath:@"/Users/xiaomage/Desktop/demo.zip" toDestination:@"/Users/xiaomage/Desktop/xx" progressHandler:^(NSString *entry, unz_file_info zipInfo, long entryNumber, long total) {
NSLog(@"%zd---%zd",entryNumber,total); } completionHandler:^(NSString *path, BOOL succeeded, NSError *error) { NSLog(@"%@",path);
}];
} @end
ios开发网络学习五:MiMEType ,多线程下载文件思路,文件的压缩和解压缩的更多相关文章
- ios开发网络学习五:输出流以及文件上传
一:输出流 #import "ViewController.h" @interface ViewController ()<NSURLConnectionDataDelega ...
- ios开发网络学习九:NSURLSessionDownloadTask实现大文件下载
一:NSURLSessionDownloadTask:实现文件下载:无法监听进度 #import "ViewController.h" @interface ViewControl ...
- ios开发网络学习十一:NSURLSessionDataTask离线断点下载(断点续传)
#import "ViewController.h" #define FileName @"121212.mp4" @interface ViewControl ...
- ios开发网络学习四:NSURLConnection大文件断点下载
#import "ViewController.h" @interface ViewController ()<NSURLConnectionDataDelegate> ...
- ios开发网络学习AFN框架的使用一:get和post请求
#import "ViewController.h" #import "AFNetworking.h" @interface ViewController () ...
- ios开发网络学习十二:NSURLSession实现文件上传
#import "ViewController.h" // ----WebKitFormBoundaryvMI3CAV0sGUtL8tr #define Kboundary @&q ...
- ios开发runtime学习五:KVC以及KVO,利用runtime实现字典转模型
一:KVC和KVO的学习 #import "StatusItem.h" /* 1:总结:KVC赋值:1:setValuesForKeysWithDictionary实现原理:遍历字 ...
- ios开发网络学习三:NSURLConnection小文件大文件下载
一:小文件下载 #import "ViewController.h" @interface ViewController ()<NSURLConnectionDataDele ...
- ios开发网络学习:一:NSURLConnection发送GET,POST请求
#import "ViewController.h" @interface ViewController ()<NSURLConnectionDataDelegate> ...
随机推荐
- ORA-01031: 权限不足
1.错误描写叙述 ORA-01031: 权限不足 2.错误原因 SQL> create user yhd identified by scott account unlock; create u ...
- Android使用蓝牙连接adb调试App
使用WiFi连接Android设备调试APP的教程非常多,可是项目中须要使用蓝牙进行通信.所以牵扯使用蓝牙调用adb. 1. 将电脑蓝牙与手机进行配对(控制面板->设备和打印机->加入 ...
- Qt样式表之盒子模型(以QSS来讲解,而不是CSS)
说起样式表,不得不提的就是盒子模型了,今天小豆君就来给大家介绍下盒子模型. 上图是一张盒子模型图. 对于一个窗口,其包括四个矩形边框.以中间的边框矩形(border)为基准,在border外侧的是外边 ...
- js03 数组
变量的自动转换=== 等同符:不会发生类型的自动转化! == 等值符:会发生类型自动转化.自动匹配!判断相等没有equals()方法,只有2个等号3个等号. <!DOCTYPE HTML PUB ...
- php全局变量的使用
php全局变量的使用 一.总结 1.php的全局变量:php的全局变量和C++,Java的全局变量不一样 2.页面嵌套php:我在html页面中嵌套php代码的时候,php的全局变量好像和C++,Ja ...
- 团队作业-Beta冲刺(3)
这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass2 这个作业要求在哪里 https://edu.cnblo ...
- 简约之美jodd--props属性使用
Prop是一个超级properties:包含了很多jdk缺失的东西:utf-8支持,宏,分区,profiles,全配置等等. 属性存储在一个或者多个*.props文件,而且它是开放的,支持多种类型的资 ...
- css实现一个缺口小三角
.square{ width:; height:; margin:0 auto; border:6px solid transparent; border-bottom: 6px solid red; ...
- iTOP-4412开发板使用
使用环境:win7 旗舰64位,VMware11 使用使用板上提供的ubuntu12.04,用VMWARE直接打开虚拟机,因为之前开发epc9600开发板,所以虚拟机网络已经设置过,加载ubuntu1 ...
- [NPM] Update published npm packages using np
When we want to update our package we need to do a few things: pull latest from our git remote, bump ...