图片压缩/解压成Zip文件
本文介绍如何将图片压缩成Zip文件,首先需要下载第三方库ZipArchive 并导入项目中。
ZipArchive 库地址:https://github.com/mattconnolly/ZipArchive
一、文档结构:
二、准备工作:
1、框架导入:
2、ZipArchive.m文件使用非ARC机制
三、代码示例:
//
// 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 *zipedFiledeBtn;
@property (weak, nonatomic) IBOutlet UILabel *statusLabel;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self downLoadImg];
}
//加载图片到本地
- (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];
}
//压缩文件
- (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"];
//实例化一个压缩文档,并创建文件
ZipArchive *za = [[ZipArchive alloc]init];
[za CreateZipFile2:zipFilePath];
//在压缩文档中添加文件
[za addFileToZip:imgPath newname:@"baidu_zipped.png"];
//关闭zip文件操作
BOOL success = [za CloseZipFile2];
if (success) {
_statusLabel.text = @"压缩成功";
}else{
_statusLabel.text = @"压缩失败";
}
}
//解压文件
- (IBAction)unzipFile:(id)sender {
NSString *docsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *zipPath = [docsPath stringByAppendingPathComponent:@"newZipFile.zip"];
ZipArchive *za = [[ZipArchive alloc]init];
//在内存中解压文件
if ([za UnzipOpenFile:zipPath]) {
//将解压的内容写到磁盘中
BOOL success = [za UnzipFileTo:docsPath overWrite:YES];
if (!success) {
_statusLabel.text = @"解压失败";
}else{
//关闭压缩文件
[za UnzipCloseFile];
_statusLabel.text = @"解压成功";
NSString *imgPath = [docsPath stringByAppendingPathComponent:@"baidu_zipped.png"];
NSData *data = [NSData dataWithContentsOfFile:imgPath];
UIImage *image = [UIImage imageWithData:data];
_imgView.image = image;
}
}else{
_statusLabel.text = @"压缩文件不存在";
}
}
//删除压缩文件
- (IBAction)deleteZipFile:(id)sender {
NSString *docsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *zipPath = [docsPath stringByAppendingPathComponent:@"newZipFile.zip"];
//创建文件管理器
NSFileManager *fm = [NSFileManager defaultManager];
//判断指定路径文件是否存在
BOOL exist = [fm fileExistsAtPath:zipPath];
if (exist) {
NSError *error = nil;
[fm removeItemAtPath:zipPath error:&error];
if (!error) {
_statusLabel.text = @"删除压缩文件成功";
}else{
_statusLabel.text = @"删除压缩文件失败";
}
}else{
_statusLabel.text = @"文件不存在";
}
}
//删除解压文件
- (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 = @"删除解压文件成功";
_imgView.image = nil;
}else{
_statusLabel.text = @"删除解压文件失败";
}
}else{
_statusLabel.text = @"文件不存在";
}
}
@end
四:效果演示
点击压缩按钮:
点击解压按钮:
删除按钮:
附:文件请在沙盒中自行查看