Compress/Unzip the image into a zip file
This article describes how to compress a picture into a zip file, first you need to download the third-party library ziparchive and import the project.
Ziparchive Library Address: https://github.com/mattconnolly/ZipArchive
First, the document structure:
Ii. Preparatory work:
1. Frame Import:
2. ziparchive.m file using non-arc mechanism
Three, code example:
viewcontroller.m// unzipimgdemo//// Created bylotheve on 15/4/10.// Copyright (c) 2015 Lotheve. All rights reserved.//#import "ViewController.h" #import "ZipArchive.h" @interface Viewcontroller () @property (weak, non Atomic) Iboutlet Uiimageview *imgview; @property (Weak, nonatomic) Iboutlet UIButton *zipbtn; @property (weak, nonatomic) Iboutlet UIButton *unzipbtn; @property ( Weak, nonatomic) Iboutlet UIButton *zipfiledebtn; @property (weak, nonatomic) Iboutlet UIButton *zipedfiledebtn; @property (Weak, nonatomic) Iboutlet UILabel *statuslabel; @end @implementation Viewcontroller-(void) viewdidload { [super viewdidload]; [Self downloadimg];}
Load picture to local-(void) downloadimg{ nsstring *urlstring = @ "Https://www.baidu.com/img/bdlogo.png"; Nsurl *url = [Nsurl urlwithstring:urlstring]; NSData *data = [NSData Datawithcontentsofurl:url]; nsmutablestring *path = [Nssearchpathfordirectoriesindomains (nscachesdirectory, Nsuserdomainmask, YES) firstObject];; nsstring *imgpath = [path stringbyappendingpathcomponent:@ "Baidu.png"]; [Data Writetofile:imgpath atomically:yes];}
Compressed file-(ibaction) ZipFile: (ID) Sender { NSString *docspath = [Nssearchpathfordirectoriesindomains ( NSDocumentDirectory, Nsuserdomainmask, YES) firstobject]; NSString *cachespath = [Nssearchpathfordirectoriesindomains (nscachesdirectory, Nsuserdomainmask, YES) firstObject]; NSString *imgpath = [Cachespath stringbyappendingpathcomponent:@ "Baidu.png"]; NSString *zipfilepath = [Docspath stringbyappendingpathcomponent:@ "Newzipfile.zip"]; Instantiate a compressed document and create a file ziparchive *za = [[ziparchive alloc]init]; [Za createzipfile2:zipfilepath]; Add a file in a compressed document [za addfiletozip:imgpath newname:@ "baidu_zipped.png"]; Close zip file operation BOOL success = [Za CloseZipFile2]; if (success) { _statuslabel.text = @ "compression succeeded"; } else{ _statuslabel.text = @ "compression failed"; }}
//Extract File-(ibaction) Unzipfile: (ID) Sender {nsstring *docspath = [ Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) firstobject]; NSString *zippath = [Docspath stringbyappendingpathcomponent:@ "Newzipfile.zip"]; Ziparchive *za = [[Ziparchive alloc]init]; Unzip the file in memory if ([za unzipopenfile:zippath]) {//writes the extracted content to the disk BOOL success = [Za unzipfileto:docspath overwr Ite:yes]; if (!success) {_statuslabel.text = @ "Decompression failed"; }else{//close compressed files [za unzipclosefile]; _statuslabel.text = @ "unzip successfully"; NSString *imgpath = [Docspath stringbyappendingpathcomponent:@ "Baidu_zipped.png"]; NSData *data = [NSData Datawithcontentsoffile:imgpath]; UIImage *image = [UIImage imagewithdata:data]; _imgview.image = image; }}else{_statuslabel.text = @ "The compressed file does not exist"; }}
Delete compressed file-(ibaction) Deletezipfile: (ID) Sender { NSString *docspath = [Nssearchpathfordirectoriesindomains ( NSDocumentDirectory, Nsuserdomainmask, YES) firstobject]; NSString *zippath = [Docspath stringbyappendingpathcomponent:@ "Newzipfile.zip"]; Create File Manager Nsfilemanager *fm = [Nsfilemanager Defaultmanager]; Determines if the specified path file exists BOOL exist = [FM Fileexistsatpath:zippath]; if (exist) { Nserror *error = nil; [FM Removeitematpath:zippath error:&error]; if (!error) { _statuslabel.text = @ "Delete compressed file succeeded"; } else{ _statuslabel.text = @ "Delete compressed file failed"; } } else{ _statuslabel.text = @ "file does not exist"; }}
Delete extract file-(ibaction) Deletezipedfile: (ID) Sender { NSString *docspath = [Nssearchpathfordirectoriesindomains ( NSDocumentDirectory, Nsuserdomainmask, YES) firstobject]; NSString *zippedfilepath = [docspathstringbyappendingpathcomponent:@ "Baidu_zipped.png"]; Nsfilemanager *FM = [Nsfilemanager Defaultmanager]; BOOL exist = [FM Fileexistsatpath:zippedfilepath]; if (exist) { Nserror *error = nil; [FM Removeitematpath:zippedfilepath error:&error]; if (!error) { _statuslabel.text = @ "Delete unzipped file succeeded"; _imgview.image = nil; } else{ _statuslabel.text = @ "Delete unzip file failed"; } } else{ _statuslabel.text = @ "file does not exist"; }}
@end
Four: Effect demonstration
Click the Compress button:
Click the Unzip button:
Delete button:
Attached: File please view in sandbox
iOS Development-image compression/decompression into zip file