一: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 ,多线程下载文件思路,文件的压缩和解压缩的更多相关文章

  1. ios开发网络学习五:输出流以及文件上传

    一:输出流 #import "ViewController.h" @interface ViewController ()<NSURLConnectionDataDelega ...

  2. ios开发网络学习九:NSURLSessionDownloadTask实现大文件下载

    一:NSURLSessionDownloadTask:实现文件下载:无法监听进度 #import "ViewController.h" @interface ViewControl ...

  3. ios开发网络学习十一:NSURLSessionDataTask离线断点下载(断点续传)

    #import "ViewController.h" #define FileName @"121212.mp4" @interface ViewControl ...

  4. ios开发网络学习四:NSURLConnection大文件断点下载

    #import "ViewController.h" @interface ViewController ()<NSURLConnectionDataDelegate> ...

  5. ios开发网络学习AFN框架的使用一:get和post请求

    #import "ViewController.h" #import "AFNetworking.h" @interface ViewController () ...

  6. ios开发网络学习十二:NSURLSession实现文件上传

    #import "ViewController.h" // ----WebKitFormBoundaryvMI3CAV0sGUtL8tr #define Kboundary @&q ...

  7. ios开发runtime学习五:KVC以及KVO,利用runtime实现字典转模型

    一:KVC和KVO的学习 #import "StatusItem.h" /* 1:总结:KVC赋值:1:setValuesForKeysWithDictionary实现原理:遍历字 ...

  8. ios开发网络学习三:NSURLConnection小文件大文件下载

    一:小文件下载 #import "ViewController.h" @interface ViewController ()<NSURLConnectionDataDele ...

  9. ios开发网络学习:一:NSURLConnection发送GET,POST请求

    #import "ViewController.h" @interface ViewController ()<NSURLConnectionDataDelegate> ...

随机推荐

  1. Day4上午解题报告

    预计分数:50 +0+0=50 实际分数:50+0+10=60 毒瘤出题人,T3不给暴力分 (*  ̄︿ ̄) T1 https://www.luogu.org/problem/show?pid=T155 ...

  2. Linux与好莱坞电影

    Linux与好莱坞电影         2009年底上映的<阿凡达>是电影特效的巅峰之作,除此之外还有<2012>每次观看之后总能让我们热血沸腾.      很早以前电影特效都 ...

  3. JavaFx EventHandler

    import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHan ...

  4. 洛谷——U10783 名字被和谐了

    https://www.luogu.org/problem/show?pid=U10783 题目背景 众所周知,我们称g是a的约数,当且仅当g是正数且a mod g = 0. 众所周知,若g既是a的约 ...

  5. linux又一次编译安装gd,添加freetype支持,解决验证码不显示问题,Fatal error: Call to undefined function imagettftext()

    问题: Fatal error: Call to undefined function Think\imagettftext() in /var/www/webreg/ThinkPHP/Library ...

  6. JS/CSS 全屏幕导航 – 从上到下动画

    <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title> ...

  7. Maven学习总结(18)——深入理解Maven仓库

    一.本地仓库(Local Repository) 本地仓库就是一个本机的目录,这个目录被用来存储我们项目的所有依赖(插件的jar包还有一些其他的文件),简单的说,当你build一个Maven项目的时候 ...

  8. [Ramda] Simple log function for debugging Compose function / Using R.tap for logging

    const log = function(x){ console.log(x); return x; } const get = R.curry(function(prop, obj){ return ...

  9. 基于am3358的led跑马灯測试

    #include <sys/ioctl.h> #include<stdio.h> #include <fcntl.h> #include <sys/types ...

  10. OC中对于属性的总结(@property)

    在没有属性之前: 对成员变量进行改动都要用到设置器:setter来改动 Person *per =[[Person alloc] init]; 对象通过设置器对成员变量内容进行修该 [per setN ...