iOS Development--Network Programming OC Articles & (eight) file compression and decompression

Source: Internet
Author: User

File compression and decompression

I. Technical PROGRAMMES
1. Third-party framework: Ssziparchive
2. Dependent Dynamic Library: Libz.dylib

Two, compression 1
1. The first method
/**
ZipFile: The final path of the resulting zip file
Directory: Compressed folder path that needs to be made
*/
[Ssziparchive Createzipfileatpath:zipfile withcontentsofdirectory:directory];

2. The first method
/**
ZipFile: The final path of the resulting zip file
Files: This is an array that contains the path to the files that need to be compressed
Files = @[@ "/users/apple/destop/1.png", @ "/users/apple/destop/3.txt"]
*/
[Ssziparchive Createzipfileatpath:zipfile withfilesatpaths:files];

Three, decompression
/**
ZipFile: The path of the zip file that needs to be unzipped
Dest: Where to unzip?
*/
[Ssziparchive Unzipfileatpath:zipfile todestination:dest];

One: File compression

1-(NSString *) MIMEType: (Nsurl *) URL2 {3     //1. Create a request4Nsurlrequest *request =[Nsurlrequest Requestwithurl:url];5     //2. Send request (return response)6Nsurlresponse *response =Nil;7[Nsurlconnection sendsynchronousrequest:request returningresponse:&response Error:nil];8     //3. Get MimeType9     returnResponse. MIMEType;Ten } One  A- (void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event - { -NSString *caches =[Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) lastobject]; the  -     //0. Get the folder you want to compress -NSString *images = [caches stringbyappendingpathcomponent:@"Images"]; -      +     //1. Create a ZIP file (compress) -NSString *zipfile = [caches stringbyappendingpathcomponent:@"Images.zip"]; +  ABOOL result =[ssziparchive createzipfileatpath:zipfile withcontentsofdirectory:images]; at     if(Result) { -NSString *mimetype =[self mimetype:[nsurl fileurlwithpath:zipfile]]; -NSData *data =[NSData Datawithcontentsoffile:zipfile]; -[Self Upload:@"Images.zip"Mimetype:mimetype Filedata:dataparams:@{@"username":@"Lisi"}]; -     } - } in  -  to- (void) Upload: (NSString *) filename mimeType: (NSString *) MimeType fileData: (NSData *) fileDataparams:(Nsdictionary *)params + { -     //1. Request Path theNsurl *url = [Nsurl urlwithstring:@"Http://192.168.15.172:8080/MJServer/upload"]; *      $     //2. Create a POST requestPanax NotoginsengNsmutableurlrequest *request =[Nsmutableurlrequest Requestwithurl:url]; -Request. HttpMethod =@"POST"; the      +     //3. Set the request body ANsmutabledata *body =[Nsmutabledata data]; the      +     //3.1. File Parameters -[Body Appenddata:icocosencode (@"--")]; $ [Body Appenddata:icocosencode (icocosfileboundary)]; $ [Body Appenddata:icocosencode (Icocosnewlien)]; -      -NSString *disposition = [NSString stringWithFormat:@"content-disposition:form-data; name=\"File\"; filename=\ "%@\"", filename]; the [Body appenddata:icocosencode (disposition)]; - [Body Appenddata:icocosencode (Icocosnewlien)];Wuyi      theNSString *type = [NSString stringWithFormat:@"Content-type:%@", MimeType]; - [Body Appenddata:icocosencode (type)]; Wu [Body Appenddata:icocosencode (Icocosnewlien)]; -      About [Body Appenddata:icocosencode (Icocosnewlien)]; $ [Body appenddata:filedata]; - [Body Appenddata:icocosencode (Icocosnewlien)]; -      -     //3.2. Non-file parameters A[paramsenumeratekeysandobjectsusingblock:^ (IDKeyIDobj, BOOL *stop) { +[Body Appenddata:icocosencode (@"--")]; the [Body Appenddata:icocosencode (icocosfileboundary)]; - [Body Appenddata:icocosencode (Icocosnewlien)]; $          theNSString *disposition = [NSString stringWithFormat:@"content-disposition:form-data; name=\"%@\"", key]; the [Body appenddata:icocosencode (disposition)]; the [Body Appenddata:icocosencode (Icocosnewlien)]; the          - [Body Appenddata:icocosencode (Icocosnewlien)]; in [Body appenddata:icocosencode ([obj description]); the [Body Appenddata:icocosencode (Icocosnewlien)]; the     }]; About      the     //3.3. End tag the[Body Appenddata:icocosencode (@"--")]; the [Body Appenddata:icocosencode (icocosfileboundary)]; +[Body Appenddata:icocosencode (@"--")]; - [Body Appenddata:icocosencode (Icocosnewlien)]; the     BayiRequest. Httpbody =body; the      the     //4. Set the request header (tell the server this time to you is the file data, tell the server is now sending a file upload request) -NSString *contenttype = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", icocosfileboundary]; -[Request Setvalue:contenttype Forhttpheaderfield:@"Content-type"]; the      the     //5. Sending the request the[Nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue Mainqueue] completionHandler:^ ( Nsurlresponse *response, NSData *data, Nserror *connectionerror) { theNsdictionary *dict =[nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutableleaves Error:nil]; -NSLog (@"%@", dict); the     }]; the}

Two: File decompression

1- (void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event2 {3Nsurl *url = [Nsurl urlwithstring:@"Http://localhost:8080/MJServer/resources/images.zip"];4Nsurlsessiondownloadtask *task = [[nsurlsession sharedsession] Downloadtaskwithurl:url completionHandler:^ (NSURL * Location, Nsurlresponse *response, Nserror *error) {5NSString *caches =[Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) lastobject];6 [ssziparchive UnzipFileAtPath:location.path todestination:caches];7     }];8 [Task resume];9}

iOS Development--Network Programming OC Chapter & (eight) file compression and decompression

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.