您的位置:首页 > 其它

文件上传分析、文件解压缩

2015-09-16 09:57 169 查看
文件上传

一、文件上传的步骤
1.设置请求头
* 目的:告诉服务器请求体里面的内容并非普通的参数,而是包含了文件参数

[request setValue:@"multipart/form-data; boundary=heima" forHTTPHeaderField:@"Content-Type"];


2.设置请求体
* 作用:存放参数(文件参数和非文件参数)
1> 非文件参数

[body appendData:HMEncode(@"--heima\r\n")];
[body appendData:HMEncode(@"Content-Disposition: form-data; name=\"username\"\r\n")];

[body appendData:HMEncode(@"\r\n")];
[body appendData:HMEncode(@"张三")];
[body appendData:HMEncode(@"\r\n")];


2> 文件参数

[body appendData:HMEncode(@"--heima\r\n")];
[body appendData:HMEncode(@"Content-Disposition: form-data; name=\"file\"; filename=\"test123.png\"\r\n")];
[body appendData:HMEncode(@"Content-Type: image/png\r\n")];

[body appendData:HMEncode(@"\r\n")];
[body appendData:imageData];
[body appendData:HMEncode(@"\r\n")];


3> 结束标记 :参数结束的标记

[body appendData:HMEncode(@"--heima--\r\n")];


二、文件的MIMEType
1.百度搜索

2.查找服务器下面的某个xml文件
apache-tomcat-6.0.41\conf\web.xml

3.加载文件时通过Reponse获得

- (NSString *)MIMEType:(NSURL *)url
{
// 1.创建一个请求
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 2.发送请求(返回响应)
NSURLResponse *response = nil;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
// 3.获得MIMEType
return response.MIMEType;
}


4.通过C语言函数

+ (NSString *)mimeTypeForFileAtPath:(NSString *)path
{
if (![[NSFileManager alloc] init] fileExistsAtPath:path]) {
return nil;
}

CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (CFStringRef)[path pathExtension], NULL);
CFStringRef MIMEType = UTTypeCopyPreferredTagWithClass (UTI, kUTTagClassMIMEType);
CFRelease(UTI);
if (!MIMEType) {
return @"application/octet-stream";
}
return NSMakeCollectable(MIMEType);
}


文件解压缩

一、技术方案
1.第三方框架:SSZipArchive
2.依赖的动态库:libz.dylib

二、压缩
1.第一个方法

/**
zipFile :产生的zip文件的最终路径
directory : 需要进行的压缩的文件夹路径
*/
[SSZipArchive createZipFileAtPath:zipFile withContentsOfDirectory:directory];


2.第二个方法

/**
zipFile :产生的zip文件的最终路径
files : 这是一个数组,数组里面存放的是需要压缩的文件的路径
files = @[@"/Users/apple/Destop/1.png", @"/Users/apple/Destop/3.txt"]
*/
[SSZipArchive createZipFileAtPath:zipFile withFilesAtPaths:files];


三、解压缩

/**
zipFile :需要解压的zip文件的路径
dest : 解压到什么地方
*/
[SSZipArchive unzipFileAtPath:zipFile toDestination:dest];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: