IOS-网络(文件压缩和解压缩)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了IOS-网络(文件压缩和解压缩)相关的知识,希望对你有一定的参考价值。

 

  1 //
  2 //  ViewController.m
  3 //  ios_0206_文件上传
  4 //
  5 //  Created by ma c on 16/2/6.
  6 //  Copyright © 2016年 博文科技. All rights reserved.
  7 //
  8 
  9 #import "ViewController.h"
 10 #import "SSZipArchive.h"
 11 
 12 #define BWFileBoundary @"----------BowenKeJi"
 13 #define BWNewLine @"\r\n"
 14 #define BWEncode(str) [str dataUsingEncoding:NSUTF8StringEncoding]
 15 
 16 @interface ViewController ()
 17 
 18 @end
 19 
 20 @implementation ViewController
 21 
 22 - (void)viewDidLoad {
 23     [super viewDidLoad];
 24     self.view.backgroundColor = [UIColor cyanColor];
 25 }
 26 
 27 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
 28 {
 29     [self createZip];
 30 }
 31 ///压缩文件
 32 - (void)createZip
 33 {
 34     NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
 35     //0.需要压缩的文件夹
 36     NSString *images = [caches stringByAppendingPathComponent:@"images"];
 37     //1.创建一个zip文件
 38     NSString *zipFile = [caches stringByAppendingPathComponent:@"images.zip"];
 39     BOOL result = [SSZipArchive createZipFileAtPath:zipFile withContentsOfDirectory:images];
 40     
 41     if (result) {
 42         NSString *MIMEType = [self MIMEType:[NSURL fileURLWithPath:zipFile]];
 43         NSData *data = [NSData dataWithContentsOfFile:zipFile];
 44         [self upload:@"images.zip" AndMIMEType:MIMEType AndfileData:data AndParams:nil];
 45     }
 46     
 47 }
 48 ///解压缩文件
 49 - (void)unZip
 50 {
 51     NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/resources/images.zip"];
 52     NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithURL:url completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
 53         
 54         NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
 55         [SSZipArchive unzipFileAtPath:location.path toDestination:caches uniqueId:nil];
 56         
 57     }];
 58     [task resume];
 59 }
 60 
 61 ///文件的MIMEType
 62 - (NSString *)MIMEType:(NSURL *)url
 63 {
 64     //1.创建一个请求
 65     NSURLRequest *request = [NSURLRequest requestWithURL:url];
 66     
 67     NSURLResponse *response = nil;
 68     //2.发送请求(返回响应)
 69     [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
 70     //3.获得MIMEType
 71     return response.MIMEType;
 72 }
 73 
 74 ///文件上传封装
 75 - (void)upload:(NSString *)filename AndMIMEType:(NSString *)mimeType AndfileData:(NSData *)fileData
 76    AndParams:(NSDictionary *)dict
 77 {
 78     // 1.请求路径
 79     NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/upload"];
 80     // 2.创建一个POST请求
 81     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
 82     request.HTTPMethod = @"POST";
 83     // 2.设置请求头(告诉服务器这次上传的是文件数据)
 84     NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",BWFileBoundary];
 85     [request setValue:contentType forHTTPHeaderField:@"Content-Type"];
 86     // 3.设置请求体
 87     NSMutableData *body = [NSMutableData data];
 88     
 89     // 4.1文件参数
 90     [body appendData:BWEncode(@"--")];
 91     [body appendData:BWEncode(BWFileBoundary)];
 92     [body appendData:BWEncode(BWNewLine)];
 93     NSString *disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"%@\"",filename];
 94     [body appendData:BWEncode(disposition)];
 95     [body appendData:BWEncode(BWNewLine)];
 96     
 97     NSString *type = [NSString stringWithFormat:@"Content-Type: %@",mimeType];
 98     [body appendData:BWEncode(type)];
 99     [body appendData:BWEncode(BWNewLine)];
100     
101     //具体内容
102     [body appendData:BWEncode(BWNewLine)];
103     [body appendData:fileData];
104     [body appendData:BWEncode(BWNewLine)];
105     
106     // 4.2非文件参数(用户名参数)
107 
108     [dict enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
109         [body appendData:BWEncode(@"--")];
110         [body appendData:BWEncode(BWFileBoundary)];
111         [body appendData:BWEncode(BWNewLine)];
112         
113         NSString *disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"",key];
114         
115         [body appendData:BWEncode(disposition)];
116         [body appendData:BWEncode(BWNewLine)];
117         
118         [body appendData:BWEncode(BWNewLine)];
119         [body appendData:BWEncode([obj description])];
120         [body appendData:BWEncode(BWNewLine)];
121 
122     }];
123     // 4.3结束标记
124     [body appendData:BWEncode(@"--")];
125     [body appendData:BWEncode(BWFileBoundary)];
126     [body appendData:BWEncode(@"--")];
127     [body appendData:BWEncode(BWNewLine)];
128     
129     request.HTTPBody = body;
130     
131     // 5.发送请求
132     [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
133         
134         NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
135         NSLog(@"%@",dict);
136     }];
137 }
138 
139 @end

 

以上是关于IOS-网络(文件压缩和解压缩)的主要内容,如果未能解决你的问题,请参考以下文章

ios开发网络学习五:MiMEType ,多线程下载文件思路,文件的压缩和解压缩

iOS中使用ZipArchive压缩和解压缩文件-备

在 iOS 中以编程方式压缩和解压缩文件?

ios ziparchive 怎么压缩文件夹?是文件夹不是文件哈

文件的压缩和解压缩

python中如何压缩和解压缩文件