zoukankan      html  css  js  c++  java
  • 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 @"
    "
     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
  • 相关阅读:
    android内存知识普及抱怨墨迹内存大的同学看过来
    ICS 截屏(Screenshot)代码流程小结
    ICS Overlay主要流程
    Android中的sp和wp指针
    Android Display buffer_handle_t的定义
    adb shell input 命令
    百度地图API之MyLocationOverlay的使用(Android)
    由Android系统智能手机解锁图案引出的排列组合问题
    <script runat=server>、<%%>和<%#%>前台是服务器方法
    Oracle 日期运算 集合
  • 原文地址:https://www.cnblogs.com/oc-bowen/p/5186496.html
Copyright © 2011-2022 走看看