来源:http://www.icodeblog.com/2012/08/13/unzipping-files-using-zip-archive/
译文(有删减):
在本教程中,我将演示如何从iOS应用程序内部压缩和解压缩文件。我们将使用一个第三方库ZipArchive来实现。
虽然有几个解决方案压缩和解压缩文件,但我觉得ZipArchive是最高效和最容易的压缩工具类。
一、设置您的项目
首先前往Google下载ZipArchive:http://code.google.com/p/ziparchive/downloads/list
然后将整个ZipArchive文件夹拖到您的工程中,这其中包括:ZipArchive.h、ZipArchive.mm、minizip文件夹。
注意:ZipZrchive不支持ARC模式,如果您的项目启用了ARC,您需要告诉编译器对ZipArchive不使用ARC。
您可以在target中的 “Build Phases”选项,然后在“Compile Sources”区域定位到ZipArchive.mm并双击它。
在弹出框中,键入-fno-objc-arc并点击完成。
最后一步是添加libz.1.2.5.dylib到您的项目中
接下来就可以正式使用ZipArchive工具了。
二、下载和解压缩文件
1. 包含ZipArchive头文件
#import "ZipArchive.h"
2. 下载一个zip文件
// 1
dispatch_queue_t queue = 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;
// 2
NSData *data = [NSData dataWithContentsOfURL:url options:0 error:&error];
if(!error)
{
// 3
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSString *zipPath = [path stringByAppendingPathComponent:@"zipfile.zip"];
[data writeToFile:zipPath options:0 error:&error];
if(!error)
{
// TODO: Unzip
}
else
{
NSLog(@"Error saving file %@",error);
}
}
else
{
NSLog(@"Error downloading zip file: %@", error);
}
});
这是一个初步的代码,从iCodeBlog下载zip文件并保存到应用程序的缓存目录。
1.创建一个调度队列,并使用默认优先级运行我们的代码。
2.快速的从网络上获取数据。
3.打开路径缓存目录并将zip文件保存到本地下载中。
现在您已经下载了zip文件到磁盘,接下来可以解压缩文件,并获取文件的内容。
3.解压缩下载的文件
ZipArchive *za = [[ZipArchive alloc] init];
// 1
if ([za UnzipOpenFile: zipPath]) {
// 2
BOOL ret = [za UnzipFileTo: path overWrite: YES];
if (NO == ret){} [za UnzipCloseFile];
// 3
NSString *imageFilePath = [path stringByAppendingPathComponent:@"photo.png"];
NSString *textFilePath = [path stringByAppendingPathComponent:@"text.txt"];
NSData *imageData = [NSData dataWithContentsOfFile:imageFilePath options:0 error:nil];
UIImage *img = [UIImage imageWithData:imageData];
NSString *textString = [NSString stringWithContentsOfFile:textFilePath
encoding:NSASCIIStringEncoding error:nil];
// 4
dispatch_async(dispatch_get_main_queue(), ^{
self.imageView.image = img;
self.label.text = textString;
1.打开文件,在内存中解压缩
2.解压缩内容写入给定的路径(缓存文件夹)
3.利用解压缩文件
4.更新用户界面(当然在主线程)新获取的数据。
三、压缩文件
现在您还可以通过ZipArchive将文件压缩成zip。
// 1
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docspath = [paths objectAtIndex:0];
// 2
paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [paths objectAtIndex:0];
// 3
NSString *zipFile = [docspath stringByAppendingPathComponent:@"newzipfile.zip"];
// 4
ZipArchive *za = [[ZipArchive alloc] init];
[za CreateZipFile2:zipFile];
// 5
NSString *imagePath = [cachePath stringByAppendingPathComponent:@"photo.png"];
NSString *textPath = [cachePath stringByAppendingPathComponent:@"text.txt"];
// 6
[za addFileToZip:imagePath newname:@"NewPhotoName.png"];
[za addFileToZip:textPath newname:@"NewTextName.txt"];
// 7
BOOL success = [za CloseZipFile2];
NSLog(@"Zipped file with result %d",success);
现在,您已经了解了如何 在iOS设备上使用的ZipArchive库完成压缩和解压缩文件。您可以下载这篇文章的示例项目。