iOS開發,ios開發教程

來源:互聯網
上載者:User

iOS開發,ios開發教程
第三方解壓縮架構——SSZipArchive

:https://github.com/samsoffes/ssziparchive
注意:需要引入libz.dylib架構

// UnzippingNSString *zipPath = @"path_to_your_zip_file";NSString *destinationPath =@"path_to_the_folder_where_you_want_it_unzipped";[SSZipArchive unzipFileAtPath:zipPath toDestination:destinationPath];// ZippingNSString *zippedPath = @"path_where_you_want_the_file_created";NSArray *inputPaths = [NSArray arrayWithObjects:                       [[NSBundle mainBundle] pathForResource:@"photo1" ofType:@"jpg"],                       [[NSBundle mainBundle] pathForResource:@"photo2" ofType:@"jpg"]                       nil];[SSZipArchive createZipFileAtPath:zippedPath withFilesAtPaths:inputPaths];

一、技術方案
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];
檔案壓縮執行個體
#import "MalJobViewController.h"#import "SSZipArchive.h"#define MalJobFileBoundary @"heima"#define MalJobNewLine @"\r\n"#define MalJobEncode(str) [str dataUsingEncoding:NSUTF8StringEncoding]@interface MalJobViewController ()@end@implementation MalJobViewController- (void)viewDidLoad{    [super viewDidLoad];}- (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;}- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{    NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];    // 0.獲得需要壓縮的檔案夾    NSString *images = [caches stringByAppendingPathComponent:@"images"];    // 1.建立一個zip檔案(壓縮)    NSString *zipFile = [caches stringByAppendingPathComponent:@"images.zip"];    BOOL result = [SSZipArchive createZipFileAtPath:zipFile withContentsOfDirectory:images];    if(result) {        NSString *MIMEType = [self MIMEType:[NSURL fileURLWithPath:zipFile]];        NSData *data = [NSData dataWithContentsOfFile:zipFile];        [self upload:@"images.zip" mimeType:MIMEType fileData:data params:@{@"username" : @"lisi"}];    }}- (void)upload:(NSString *)filename mimeType:(NSString *)mimeType fileData:(NSData *)fileData params:(NSDictionary *)params{    // 1.請求路徑    NSURL *url = [NSURL URLWithString:@"http://192.168.15.172:8080/Server/upload"];    // 2.建立一個POST請求    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];    request.HTTPMethod = @"POST";    // 3.佈建要求體    NSMutableData *body = [NSMutableData data];    // 3.1.檔案參數    [body appendData:MalJobEncode(@"--")];    [body appendData:MalJobEncode(MalJobFileBoundary)];    [body appendData:MalJobEncode(MalJobNewLine)];    NSString *disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"%@\"", filename];    [body appendData:MalJobEncode(disposition)];    [body appendData:MalJobEncode(MalJobNewLine)];    NSString *type = [NSString stringWithFormat:@"Content-Type: %@", mimeType];    [body appendData:MalJobEncode(type)];    [body appendData:MalJobEncode(MalJobNewLine)];    [body appendData:MalJobEncode(MalJobNewLine)];    [body appendData:fileData];    [body appendData:MalJobEncode(MalJobNewLine)];    // 3.2.非檔案參數    [params enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {        [body appendData:MalJobEncode(@"--")];        [body appendData:MalJobEncode(MalJobFileBoundary)];        [body appendData:MalJobEncode(MalJobNewLine)];        NSString *disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"", key];        [body appendData:MalJobEncode(disposition)];        [body appendData:MalJobEncode(MalJobNewLine)];        [body appendData:MalJobEncode(MalJobNewLine)];        [body appendData:MalJobEncode([obj description])];        [body appendData:MalJobEncode(MalJobNewLine)];    }];    // 3.3.結束標記    [body appendData:MalJobEncode(@"--")];    [body appendData:MalJobEncode(MalJobFileBoundary)];    [body appendData:MalJobEncode(@"--")];    [body appendData:MalJobEncode(MalJobNewLine)];    request.HTTPBody = body;    // 4.佈建要求頭(告訴伺服器這次傳給你的是檔案資料,告訴伺服器現在發送的是一個檔案上傳請求)    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", MalJobFileBoundary];    [request setValue:contentType forHTTPHeaderField:@"Content-Type"];    // 5.發送請求    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];        NSLog(@"%@", dict);    }];}@end
檔案解壓縮執行個體
#import "MalJobViewController.h"#import "SSZipArchive.h"@implementation MalJobViewController- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.}- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{    NSURL *url = [NSURL URLWithString:@"http://localhost:8080/Server/resources/images.zip"];    NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithURL:url completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {        NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];        [SSZipArchive unzipFileAtPath:location.path toDestination:caches];    }];    [task resume];}@end

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.