標籤:
檔案壓縮與解壓
一、技術方案
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 *)event13 {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 *)params32 {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 *)event2 {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 }
iOS開發——網路編程OC篇&(八)檔案壓縮與解壓