IOS开发—图片压缩/解压成Zip文件

图片压缩/解压成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

四:效果演示

点击压缩按钮:


点击解压按钮:


删除按钮:


附:文件请在沙盒中自行查看


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值