美文网首页实用小功能
iOS 使用ZipArchive解压缩和压缩文件

iOS 使用ZipArchive解压缩和压缩文件

作者: Johnny_Chang | 来源:发表于2016-01-15 17:28 被阅读7503次

为什么我需要解压缩文件

有许多原因能解释为什么我要在工程中使用压缩和解压缩功能,下面是几个常见的原因:

苹果App Store的50M下载限制

苹 果公司出于流量的考虑,规定在非WIFI环境下,限制用户只能下载小于50M的应用或游戏。这样一来,对于一些数据或数据包较大的应用,我们只能尽量减小 应用二进制包的体积。而把数据打包到zip中,这样App可以通过网络下载数据包,解压出所需要的内容,而且这样也可以动态的更新内容。

动态更新内容

这 一点在上面已经提过了。如果应用所需要的资源需要动态更新,一种常见的做法是更新资源,重新打包,重新提交到App store,这样做你需要等待漫长的审核、上架时间。一般情况下是一周左右的时间。更好的方法是将这些资源打包放置在服务器上,App从服务器(或者云存 储)上下载,然后解压。这样做的好处显而易见,那就是可以快速更新,动态更新,不需要重新打包、上传、审核,省时省力。

从Web上下载zip文件

Safari和邮件程序都不支持zip的查看,通过ZipArchive你就可以为你的设备增加查看zip文件的能力了,尽管App Store里已经有一些App支持这些功能了。

主要看了两个帖子帖子一帖子二,然后用cocoapods下了下zipArchiver的三方库,就可以了。

我的代码:

#import"ViewController.h"

#import

@interfaceViewController()

@end

@implementationViewController

{

NSString*path;

NSString*zipPath;

ZipArchive*zip;

}

- (void)viewDidLoad {

[superviewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

//下载解压缩zip文件

[selfdownloadZipFiles];

}

- (void)downloadZipFiles

{

dispatch_queue_tqueue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);

dispatch_async(queue, ^{

NSURL*url = [NSURL URLWithString:@"http://www.icodeblog.com/wp-content/uploads/2012/08/zipfile.zip"];

NSError*error =nil;

NSData*data = [NSData dataWithContentsOfURL:urloptions:0error:nil];

if(!error)

{

NSArray*pathes =NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES);

path= [pathes objectAtIndex:0];

zipPath= [path stringByAppendingPathComponent:@"zipfile.zip"];

[data writeToFile:zipPathoptions:0error:nil];

//解压zip文件

[selfreleaseZipFiles];

}

});

}

- (void)releaseZipFiles

{

zip= [[ZipArchive alloc]init];

//1.在内存中解压缩文件,

if([zip UnzipOpenFile:zipPath])

{

//2.将解压缩的内容写到缓存目录中

BOOLret = [zip UnzipFileTo:pathoverWrite:YES];

if(NO== ret) {

[zip UnzipCloseFile];

}

//3.使用解压缩后的文件

NSString*imageFilePath = [path stringByAppendingPathComponent:@"photo.png"];

NSString*textPath = [path stringByAppendingPathComponent:@"text.txt"];

NSData*imageData = [NSData dataWithContentsOfFile:imageFilePathoptions:0error:nil];

UIImage*image = [UIImageimageWithData:imageData];

NSString*string = [NSString stringWithContentsOfFile:textPathencoding:NSASCIIStringEncodingerror:nil];

//4.更新UI

dispatch_async(dispatch_get_main_queue(), ^{

_headImageView.image= image;

_IntroduceLabel.text= string;

});

//压缩zip文件,压缩文件名

NSString*createZipPath = [path stringByAppendingPathComponent:@"myFile.zip"];

//判断文件是否存在,如果存在则删除文件

NSFileManager * fileManager = [NSFileManager defaultManager];

@try

{

if([fileManager fileExistsAtPath:createZipPath])

{

if(![fileManager removeItemAtPath:createZipPath error:nil])

{

CCLog(@"Delete zip file failure.");

}

}

}

@catch(NSException * exception) {

CCLog(@"%@",exception);

}

//判断需要压缩的文件是否为空

if(string.length<1)

{

CCLog(@"The files want zip is nil.");

return nil;

}

[zip CreateZipFile2:createZipPath];

//解压缩文件名

[zip addFileToZip:textPathnewname:@"Words.txt"];

[zip CloseZipFile2];

}

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

相关文章

网友评论

  • z益达:有demo么
  • 不管you多苦:direct.h file not found这个是怎么解决的
  • LV大树:能解压但不能知道解压速度。
    LV大树:好像是bool返回的。单线程操作。耗时操作。
    LV大树:不知道什么时候解压完。没回调。
  • lidaozou:你好 我按照你的方法解压一个图片,为什么解压后是个空的呢?
    小马哥丶:我也是这种情况,压缩以后成功,在解压.zip 就是空的,你解决了吗?
    Johnny_Chang:@lidaozou 平时很少有上简书,我当初用是没有问题的,你看看你的路径对吗

本文标题:iOS 使用ZipArchive解压缩和压缩文件

本文链接:https://www.haomeiwen.com/subject/avuhkttx.html