标签:
文件压缩与解压
一、技术方案
1.第三方框架:SSZipArchive
2.依赖的动态库:libz.dylib
二、压缩1
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];
一:文件压缩
1 - (NSString *)MIMEType:(NSURL *)url 2 { 3 // 1.创建一个请求 4 NSURLRequest *request = [NSURLRequest requestWithURL:url]; 5 // 2.发送请求(返回响应) 6 NSURLResponse *response = nil; 7 [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil]; 8 // 3.获得MIMEType 9 return response.MIMEType; 10 } 11 12 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 13 { 14 NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; 15 16 // 0.获得需要压缩的文件夹 17 NSString *images = [caches stringByAppendingPathComponent:@"images"]; 18 19 // 1.创建一个zip文件(压缩) 20 NSString *zipFile = [caches stringByAppendingPathComponent:@"images.zip"]; 21 22 BOOL result = [SSZipArchive createZipFileAtPath:zipFile withContentsOfDirectory:images]; 23 if(result) { 24 NSString *MIMEType = [self MIMEType:[NSURL fileURLWithPath:zipFile]]; 25 NSData *data = [NSData dataWithContentsOfFile:zipFile]; 26 [self upload:@"images.zip" mimeType:MIMEType fileData:data params:@{@"username" : @"lisi"}]; 27 } 28 } 29 30 31 - (void)upload:(NSString *)filename mimeType:(NSString *)mimeType fileData:(NSData *)fileData params:(NSDictionary *)params 32 { 33 // 1.请求路径 34 NSURL *url = [NSURL URLWithString:@"http://192.168.15.172:8080/MJServer/upload"]; 35 36 // 2.创建一个POST请求 37 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 38 request.HTTPMethod = @"POST"; 39 40 // 3.设置请求体 41 NSMutableData *body = [NSMutableData data]; 42 43 // 3.1.文件参数 44 [body appendData:iCocosEncode(@"--")]; 45 [body appendData:iCocosEncode(iCocosFileBoundary)]; 46 [body appendData:iCocosEncode(iCocosNewLien)]; 47 48 NSString *disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"%@\"", filename]; 49 [body appendData:iCocosEncode(disposition)]; 50 [body appendData:iCocosEncode(iCocosNewLien)]; 51 52 NSString *type = [NSString stringWithFormat:@"Content-Type: %@", mimeType]; 53 [body appendData:iCocosEncode(type)]; 54 [body appendData:iCocosEncode(iCocosNewLien)]; 55 56 [body appendData:iCocosEncode(iCocosNewLien)]; 57 [body appendData:fileData]; 58 [body appendData:iCocosEncode(iCocosNewLien)]; 59 60 // 3.2.非文件参数 61 [params enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { 62 [body appendData:iCocosEncode(@"--")]; 63 [body appendData:iCocosEncode(iCocosFileBoundary)]; 64 [body appendData:iCocosEncode(iCocosNewLien)]; 65 66 NSString *disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"", key]; 67 [body appendData:iCocosEncode(disposition)]; 68 [body appendData:iCocosEncode(iCocosNewLien)]; 69 70 [body appendData:iCocosEncode(iCocosNewLien)]; 71 [body appendData:iCocosEncode([obj description])]; 72 [body appendData:iCocosEncode(iCocosNewLien)]; 73 }]; 74 75 // 3.3.结束标记 76 [body appendData:iCocosEncode(@"--")]; 77 [body appendData:iCocosEncode(iCocosFileBoundary)]; 78 [body appendData:iCocosEncode(@"--")]; 79 [body appendData:iCocosEncode(iCocosNewLien)]; 80 81 request.HTTPBody = body; 82 83 // 4.设置请求头(告诉服务器这次传给你的是文件数据,告诉服务器现在发送的是一个文件上传请求) 84 NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", iCocosFileBoundary]; 85 [request setValue:contentType forHTTPHeaderField:@"Content-Type"]; 86 87 // 5.发送请求 88 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { 89 NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]; 90 NSLog(@"%@", dict); 91 }]; 92 }
二:文件解压
1 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 2 { 3 NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/resources/images.zip"]; 4 NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithURL:url completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) { 5 NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; 6 [SSZipArchive unzipFileAtPath:location.path toDestination:caches]; 7 }]; 8 [task resume]; 9 }
标签:
原文地址:http://www.cnblogs.com/iCocos/p/4550331.html