Categories

Posts Tagged ‘minizip’

Objective-C class for zip/unzip zip format files

This is a simple class for compressing and extracting files. It works depend on minizip, which is a open source zip format library. And it’s included in the attachment.

The major class name is ZipArchive, it’s easy to use, you can declare a instance and call initialize functions, and then call addFileToZip or UnzipFileTo to finish compression or uncompression.

The code below shows you how to use it.

To create and add files to a zip

1
2
3
4
5
6
7
		BOOL ret = [zip CreateZipFile2:l_zipfile];
		ret = [zip addFileToZip:l_photo newname:@"photo.jpg"];
		if( ![zip CloseZipFile2] )
		{
			l_zipfile = @"";
		}
		[zip release];

Extract files in a zip file to special directory, if the directory does not exist, the class will create it automatically. also if you pass ‘overWrite’ as ‘YES’ it will overwrite files already exist. You can also implement the methods of ZipArchiveDelegate to give more choices for overwriting.

1
2
3
4
5
6
7
8
9
10
11
	ZipArchive* za = [[ZipArchive alloc] init];
	if( [za UnzipOpenFile:@"/Volumes/data/testfolder/Archive.zip"] )
	{
		BOOL ret = [za UnzipFileTo:@"/Volumes/data/testfolder/extract" overWrite:YES];
		if( NO==ret )
		{
		}
		[za UnzipCloseFile];
	}
 
	[za release];

zip/unzip wrapper class for Objective-C

iPhone开发之打包zip文件

程序需要往服务器上上传文件, 因为iPhone用户往往是用gprs或者edge网络,为了节约流量以及加快上传速度,所以只好将要上传的文件打包成zip文件,这样体积小了, 也为用户节约了时间和金钱。 开始的时候抱有意思希望去挖掘SDK文档, 未果, sdk不提供zip相关接口,在apple论坛打听了一下,很多dx给的建议是用apple script在后台打包, 对此领域不熟悉,放弃。 好在iPhone的官方SDK支持zLib库,这就好了, 找来minizip,一个封装的挺好的C/C++ zip库, 动手创建Objective-C对象封装之, 只需要几行代码即可完成, 简单实用。

使用方法如下

1
2
3
4
5
6
7
		BOOL ret = [zip CreateZipFile2:l_zipfile];
		ret = [zip addFileToZip:l_photo newname:@"photo.jpg"];
		if( ![zip CloseZipFile2] )
		{
			l_zipfile = @"";
		}
		[zip release];

其中 l_photo是之源文件路径,  @”photo.jpg” 是在新的文件名(不带路径)

之前没有实现 解压缩的代码, 今天补上了, 使用起来也和压缩差不多,下面时用法演示

1
2
3
4
5
6
7
8
9
10
11
	ZipArchive* za = [[ZipArchive alloc] init];
	if( [za UnzipOpenFile:@"/Volumes/data/testfolder/Archive.zip"] )
	{
		BOOL ret = [za UnzipFileTo:@"/Volumes/data/testfolder/extract" overWrite:YES];
		if( NO==ret )
		{
		}
		[za UnzipCloseFile];
	}
 
	[za release];

代码在附件中

Zip格式解压缩Objective C封装类