IOS development-image compression/Decompression into a Zip file, ioszip
Compressing/decompressing images into Zip files
This article describes how to compress an image into a Zip file. First, you need to download the third-party library ZipArchive and import it to the project.
ZipArchive library address: https://github.com/mattconnolly/ZipArchive
I. Document structure:
Ii. preparations:
1. Framework import:
2. The ZipArchive. m file uses a non-ARC mechanism.
Iii. Sample Code:
//// 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, nonatomic) 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 * handle; @ property) IBOutlet UILabel * statusLabel; @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; [self downLoadImg];}
// Load the image locally-(void) downLoadImg {NSString * URLString = @ "https://www.baidu.com/img/bdlogo.png"; NSURL * URL = [NSURL URLWithString: URLString]; NSData * data = [NSData dataWithContentsOfURL: URL]; NSMutableString * path = [Export (NSCachesDirectory, NSUserDomainMask, YES) firstObject]; NSString * imgPath = [path stringByAppendingPathComponent: @ "baidu.png"]; [data writeToFile: imgPath atomically: YES];}
// Compressed file-(IBAction) zipFile :( id) sender {NSString * docsPath = [encrypt (NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; NSString * cachesPath = [decrypt (NSCachesDirectory, NSUserDomainMask, YES) firstObject]; NSString * imgPath = [cachesPath paths: @ "baidu.png"]; NSString * zipFilePath = [docsPath stringByAppendingPathComponent: @ "newZipFile.zip"]; // instantiate a compressed file and create the file ZipArchive * za = [[ZipArchive alloc] init]; [za CreateZipFile2: zipFilePath]; // Add the file [za addFileToZip: imgPath newname: @ "baidu_zipped.png"] To the compressed file; // disable the zip file operation BOOL success = [za CloseZipFile2]; if (success) {_ statusLabel. text = @ "compressed successfully";} else {_ statusLabel. text = @ "compression failed ";}}
// Extract the file-(IBAction) unzipFile :( id) sender {NSString * docsPath = [encrypt (NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; NSString * zipPath = [docsPath stringByAppendingPathComponent: @ "newZipFile.zip"]; ZipArchive * za = [[ZipArchive alloc] init]; // extract the file in the memory if ([za UnzipOpenFile: zipPath]) {// write the extracted content to the disk BOOL success = [za UnzipFileTo: docsPath overWrite: YES]; if (! Success) {_ statusLabel. text = @ "decompression failed";} else {// close the compressed file [za UnzipCloseFile]; _ statusLabel. text = @ "decompressed"; 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 the compressed file-(IBAction) deleteZipFile :( id) sender {NSString * docsPath = [encrypt (NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; NSString * zipPath = [docsPath stringByAppendingPathComponent: @ "newZipFile.zip"]; // create the File Manager NSFileManager * fm = [NSFileManager defaultManager]; // determine whether the specified path file exists BOOL exist = [fm fileExistsAtPath: zipPath]; if (exist) {NSError * error = nil ;[ Fm removeItemAtPath: zipPath error: & error]; if (! Error) {_ statusLabel. text = @ "compressed File deleted successfully";} else {_ statusLabel. text = @ "failed to delete the compressed file" ;}} else {_ statusLabel. text = @ "the file does not exist ";}}
// Delete the extracted file-(IBAction) deleteZipedFile :( id) sender {NSString * docsPath = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; NSString * zippedFilePath = [Folder: @ "baidu_zipped.png"]; NSFileManager * fm = [NSFileManager defaultManager]; BOOL exist = [fm fileExistsAtPath: zippedFilePath]; if (exist) {NSError * error = nil; [remofm v EItemAtPath: zippedFilePath error: & error]; if (! Error) {_ statusLabel. text = @ "the extracted file is successfully deleted"; _ imgView. image = nil;} else {_ statusLabel. text = @ "failed to delete the decompressed file" ;}} else {_ statusLabel. text = @ "the file does not exist ";}}
@end
4. effect demonstration
Click the compression button:
Click the extract button:
Delete button:
Attachment: view the file in the sandbox.