IOS development and ios development tutorial

Source: Internet
Author: User

IOS development and ios development tutorial
Third-party decompression framework-SSZipArchive

: Https://github.com/samsoffes/ssziparchive
Note: The libz. dylib framework must be introduced.

// 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];

I. Technical Solutions
1. Third-party framework: SSZipArchive
2. dependent dynamic library: libz. dylib

Ii. Compression 1
1. The first method
/**
ZipFile: The final path of the generated zip file
Directory: the path of the folder to be compressed
*/

[SSZipArchive createZipFileAtPath:zipFile withContentsOfDirectory:directory];

2. The first method
/**
ZipFile: The final path of the generated zip file
Files: This is an array that stores the path of the files to be compressed.
Files = @ [@ "/Users/apple/Destop/1.png", @ "/Users/apple/Destop/3.txt"]
*/

[SSZipArchive createZipFileAtPath:zipFile withFilesAtPaths:files];

Iii. Decompression
/**
ZipFile: Path of the zip file to be decompressed
Dest: Where to decompress the package
*/

[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 {[super viewDidLoad];}-(NSString *) MIMEType :( NSURL *) url {// 1. create a request NSURLRequest * request = [NSURLRequest requestWithURL: url]; // 2. send a request (response) NSURLResponse * response = nil; [NSURLConnection sendSynchronousRequest: request returningResponse: & response error: nil]; // 3. obtain MIMEType return response. MIMEType;}-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event {NSString * caches = [events (NSCachesDirectory, NSUserDomainMask, YES) lastObject]; // 0. obtain the NSString * images = [caches stringByAppendingPathComponent: @ "images"]; // 1. create a zip file (Compressed) 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. request Path NSURL * url = [NSURL URLWithString: @ "http: // 192.168.15.172: 8080/Server/upload"]; // 2. create a POST request NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL: url]; request. HTTPMethod = @ "POST"; // 3. set the Request body NSMutableData * body = [NSMutableData data]; // 3. 1. file parameter [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. non-file parameter [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. end tag [body appendData: MalJobEncode (@ "--")]; [body appendData: MalJobEncode (MalJobFileBoundary)]; [body appendData: MalJobEncode (@ "--")]; [body appendData: MalJobEncode (MalJobNewLine)]; request. HTTPBody = body; // 4. set the Request Header (telling the server that the file data is sent to you this time, and telling the server that a file upload request is sent now) NSString * contentType = [NSString stringWithFormat: @ "multipart/form-data; boundary = % @", MalJobFileBoundary]; [request setValue: contentType forHTTPHeaderField: @ "Content-Type"]; // 5. send request [NSURLConnection failed: request queue: [inclumainqueue] completionHandler: ^ (NSURLResponse * response, NSData * data, NSError * connectionError) {NSDictionary * dict = [NSJSONSerialization failed: data options: NSJSONReadingMutableLeaves error: nil]; NSLog (@ "% @", dict) ;}] ;}@ end
File extraction instance
#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

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.