iOS development-File compression and decompression

Source: Internet
Author: User

Third party decompression Frame--ssziparchive

: https://github.com/samsoffes/ssziparchive
Note: The Libz.dylib framework needs to be introduced

//unzippingNSString*zippath = @"Path_to_your_zip_file";NSString*destinationpath [email protected]"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 = [NsarrayArraywithobjects: [[NSBundleMainbundle] pathforresource:@"Photo1"oftype:@"JPG"],                       [[NSBundleMainbundle] pathforresource:@"Photo2"oftype:@"JPG"]Nil]; [Ssziparchive Createzipfileatpath:zippedpath withfilesatpaths:inputpaths];

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];
File Compression instance
#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{[SuperViewdidload];} - (NSString*) MIMEType: (Nsurl*) url{//1. Create a request    nsurlrequest*request = [nsurlrequestRequestwithurl:url];//2. Send request (return response)Nsurlresponse *response =Nil; [nsurlconnectionSendsynchronousrequest:request Returningresponse:&response Error:Nil];//3. Get MimeType    returnResponse. MIMEType;} - (void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event{NSString*caches = [Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask,YES) Lastobject];//0. Get the folder you want to compress    NSString*images = [Caches stringbyappendingpathcomponent:@"Images"];//1. Create a ZIP file (compressed)    NSString*zipfile = [Caches stringbyappendingpathcomponent:@"Images.zip"];BOOLresult = [ssziparchive createzipfileatpath:zipfile withcontentsofdirectory:images];if(Result) {NSString*mimetype = [ Selfmimetype:[NsurlFileurlwithpath:zipfile]];        NSData *data = [NSData datawithcontentsoffile:zipfile]; [ Selfupload:@"Images.zip"Mimetype:mimetype Filedata:data params:@{@"username": @"Lisi"}]; }}- (void) Upload: (NSString*) filename MimeType: (NSString*) MimeType FileData: (NSData *) FileData params: (nsdictionary*) params{//1. Request Path    Nsurl*url = [Nsurlurlwithstring:@"Http://192.168.15.172:8080/Server/upload"];//2. Create a POST requestNsmutableurlrequest *request = [Nsmutableurlrequest Requestwithurl:url]; Request. HttpMethod= @"POST";//3. Set the request bodyNsmutabledata *body = [Nsmutabledata data];//3.1. File Parameters[Body Appenddata:maljobencode (@"--")];    [Body Appenddata:maljobencode (maljobfileboundary)]; [Body Appenddata:maljobencode (maljobnewline)];NSString*disposition = [NSStringstringwithformat:@"Content-disposition:form-data; Name=\ "File\"; Filename=\ "%@\" ", filename];    [Body Appenddata:maljobencode (disposition)]; [Body Appenddata:maljobencode (maljobnewline)];NSString*type = [NSStringstringwithformat:@"Content-type:%@", MimeType];    [Body Appenddata:maljobencode (type)];    [Body Appenddata:maljobencode (maljobnewline)];    [Body Appenddata:maljobencode (maljobnewline)];    [Body appenddata:filedata]; [Body Appenddata:maljobencode (maljobnewline)];//3.2. Non-file parameters[Params enumeratekeysandobjectsusingblock:^ (IDKeyIDObjBOOL*stop) {[Body Appenddata:maljobencode (@"--")];        [Body Appenddata:maljobencode (maljobfileboundary)]; [Body Appenddata:maljobencode (maljobnewline)];NSString*disposition = [NSStringstringwithformat:@"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. End tag[Body Appenddata:maljobencode (@"--")];    [Body Appenddata:maljobencode (maljobfileboundary)]; [Body Appenddata:maljobencode (@"--")];    [Body Appenddata:maljobencode (maljobnewline)]; Request. Httpbody= Body;//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 = [NSStringstringwithformat:@"Multipart/form-data; boundary=%@ ", Maljobfileboundary]; [Request Setvalue:contenttype forhttpheaderfield:@"Content-type"];//5. Send Request[nsurlconnectionSendasynchronousrequest:request Queue:[nsoperationqueue Mainqueue] completionhandler:^ (NSURLResponse *response, NSData *data,Nserror*connectionerror) {nsdictionary*dict = [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutableleaves Error:Nil];NSLog(@"%@", dict); }];}@end
File Decompression Instance
#import "MalJobViewController.h" #import "SSZipArchive.h"  @implementation maljobviewcontroller - (void) viewdidload{[SuperViewdidload];additional setup after loading the view, typically from a nib.}- (void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event{Nsurl*url = [Nsurlurlwithstring:@"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. PathTodestination:caches];    }]; [Task resume];}@end

iOS development-File compression and decompression

Related Article

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.