Advertise Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

sdkIQ for iPhone
($4.99)

Your First iPhone App
($1.99)

iPhone Code Generator
($9.99)

Dual Matches
($0.99)

Calcuccino Programmers' Calculator
($2.99)

SDKtoday
(free)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 12-10-2008, 02:09 AM   #1 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 35
Default a simple Objective-C class for zip/unzip zip format files

I've put the source code here: http://code.google.com/p/ziparchive/

I wrote 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.

it's simple but meet my own requirement

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.

download the source here

for more detailed info, please visit my article

Last edited by acsolu; 02-02-2009 at 08:20 PM.
acsolu is offline   Reply With Quote
Old 12-28-2008, 08:43 PM   #2 (permalink)
Registered Member
 
Join Date: Dec 2008
Posts: 429
Default

Excellent, thank you so much. How difficult is it to also add functions to unzip objects in memory (eg NSData) ?
lbendlin is offline   Reply With Quote
Old 12-28-2008, 10:41 PM   #3 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 35
Default

Quote:
Originally Posted by lbendlin View Post
Excellent, thank you so much. How difficult is it to also add functions to unzip objects in memory (eg NSData) ?

it's easy, just replace line 164 in ZipArchive.mm with
NSMutableData* data = [[NSMutableData alloc] init];

and replace line 170 with
[data appendBytes:buffer length:read]

then you can handle the data in memory by yourselves
acsolu is offline   Reply With Quote
Old 12-28-2008, 11:22 PM   #4 (permalink)
New Member
 
Join Date: Aug 2008
Location: Austin, TX
Posts: 239
Default

I don't need this at the moment but who knows , it's great to have in the library. Thanks for the class!
__________________
iNeedStuff shopping assistant - get in, get out, get on with your life
ayasin is offline   Reply With Quote
Old 12-28-2008, 11:47 PM   #5 (permalink)
Registered Member
 
Join Date: Aug 2008
Posts: 80
Default

thanks acsolu! this fills a hole that has been sitting empty for a while! I'll send you some free licenses to my app via PM once it is approved.
ecume is offline   Reply With Quote
Old 12-29-2008, 01:18 PM   #6 (permalink)
Registered Member
 
Join Date: Dec 2008
Posts: 429
Default

Quote:
Originally Posted by acsolu View Post
it's easy, just replace line 164 in ZipArchive.mm with
NSMutableData* data = [[NSMutableData alloc] init];

and replace line 170 with
[data appendBytes:buffer length:read]

then you can handle the data in memory by yourselves
Right, but that would still expect the ZIP to be a physical file. I wanted to avoid writing to the flash altogether (given the poor performance and limited life etc), and hold both the ZIP and the contained file data in NSData objects. But from the looks of it this would mean rewriting a lot of functions, and I am not yet experienced enough for that.
lbendlin is offline   Reply With Quote
Old 12-29-2008, 01:20 PM   #7 (permalink)
Administrator
 
Chris Stewart's Avatar
 
Join Date: Mar 2008
Location: Richmond, VA
Age: 27
Posts: 745
Default

Excellent contribution! You should add this to Google Code, or somewhere similar, so others can contribute to it and have it evolve.
Chris Stewart is offline   Reply With Quote
Old 12-29-2008, 01:56 PM   #8 (permalink)
Registered Member
 
Join Date: Dec 2008
Posts: 429
Default

For what it's worth, here is the code to unpack the physical zip file into a NSData structure. Caveat: will only work correctly if the ZIP contains one file.

Code:
-(NSData*) UnzipFileToData {
	NSMutableData* data = [[NSMutableData alloc] init];
	int ret = unzGoToFirstFile( _unzFile );
	unsigned char		buffer[4096] = {0};
	if( ret!=UNZ_OK )
	{
		[self OutputErrorMessage:@"Failed to go to first file"];
		return data;
	}
	
	ret = unzOpenCurrentFile( _unzFile );
	if( ret!=UNZ_OK )
	{
		[self OutputErrorMessage:@"Error opening file"];
		return data;
	}
	// reading data and write to data object
	int read = 1 ;
	while( read > 0 )
	{
		read=unzReadCurrentFile(_unzFile, buffer, 4096);
		if( read > 0 )
		{
			[data appendBytes:buffer length:read];
		}
		else if( read<0 )
		{
			[self OutputErrorMessage:@"Failed to reading zip file"];
			break;
		}
		else 
			break;				
	}
	unzCloseCurrentFile( _unzFile );
	ret = unzGoToNextFile( _unzFile );
	return data;
}
lbendlin is offline   Reply With Quote
Old 01-23-2009, 10:18 AM   #9 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 192
Default

Can this handle multiple files inside a single zip archive?

I've seen other zip libs that do this in theory, but the unzipped data is simply a binary concatenation of all the files.

If not, is there a tar lib I can use?
jsonli is offline   Reply With Quote
Old 01-23-2009, 10:44 AM   #10 (permalink)
Senior Member
 
Join Date: Jan 2009
Location: Florida, USA
Posts: 471
Default

Is all the code free from copyright and patent issues?
Slayer5150 is offline   Reply With Quote
Old 01-23-2009, 10:46 AM   #11 (permalink)
Registered Member
 
Join Date: Dec 2008
Posts: 495
Default

Stellar, thanks for sharing that!
__________________
My Apps on AppStore : gScale (guitar scales reference), eMaze, eMaze Lite, eTimesheet
exorcyze is offline   Reply With Quote
Old 01-23-2009, 10:58 AM   #12 (permalink)
Registered Member
 
Join Date: Aug 2008
Posts: 80
Default

bump for jsonli's question. The unzipped data with this class does not respect the zipped data hierarchy - it unzips everything to one path...
ecume is offline   Reply With Quote
Old 01-23-2009, 12:51 PM   #13 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 192
Default

Quote:
Originally Posted by ecume View Post
bump for jsonli's question. The unzipped data with this class does not respect the zipped data hierarchy - it unzips everything to one path...
But it's able to otherwise handle multiple files inside the archive then?
jsonli is offline   Reply With Quote
Old 03-09-2009, 09:42 AM   #14 (permalink)
Registered Member
 
Join Date: Aug 2008
Posts: 83
Default

I can't figure out how to use the code in an Xcode project. Help!

(The problem is there are 2 mains and lots of replicated functions in minizip.c and miniunz.c so I can't build it)
indiekiduk is offline   Reply With Quote
Old 03-09-2009, 05:09 PM   #15 (permalink)
Registered Member
 
Join Date: Dec 2008
Posts: 429
Default

you have to keep the subfolder structure intact. Include only the files that you actually need for your task.
lbendlin is offline   Reply With Quote
Old 03-09-2009, 08:12 PM   #16 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 35
Default

Quote:
Originally Posted by indiekiduk View Post
I can't figure out how to use the code in an Xcode project. Help!

(The problem is there are 2 mains and lots of replicated functions in minizip.c and miniunz.c so I can't build it)

delete the files minizip.c , miniunz.c and iowin32.c & iowin32.h and remember add the libz.x.x.x.dylib to frameworks where x.x.x is the version of zip library
acsolu is offline   Reply With Quote
Old 03-16-2009, 10:56 PM   #17 (permalink)
Registered Member
 
Join Date: Aug 2008
Posts: 83
Default

Quote:
Originally Posted by acsolu View Post
delete the files minizip.c , miniunz.c and iowin32.c & iowin32.h and remember add the libz.x.x.x.dylib to frameworks where x.x.x is the version of zip library
ok thanks removed those files and got it working. Also had to remove the ../../minizip from the includes in ZipArchive.m.
indiekiduk is offline   Reply With Quote
Old 04-01-2009, 01:27 PM   #18 (permalink)
New Member
 
Join Date: Jul 2008
Posts: 69
Default

I am trying to download a remote zip file and unzip and open the file.

Here's my code:
Code:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
	NSFileManager *fileManager = [NSFileManager defaultManager];
	NSString *filePath = [NSString stringWithFormat:@"%@/myfile.txt.gz", [Utility getDocumentsPath]];
	[fileManager createFileAtPath:filePath contents:reportData attributes:nil];
	
	if([fileManager fileExistsAtPath:filePath]) {
		NSLog(@"File exists at path: %@", filePath);
	} else {
		NSLog(@"File does not exists at path: %@", filePath);
	}
	
	ZipArchive *zipArchive = [[ZipArchive alloc] init];
	if([zipArchive UnzipOpenFile:filePath]) {
		NSLog(@"Archive Open Success");
	} else  {
		NSLog(@"Failure To Open Archive");
	}
}
Which produces the following output:
2009-04-01 11:17:29.334 myApp[1042:20b] File exists at path: /Users/myname/Library/Application Support/iPhone Simulator/User/Applications/F0DC6BBC-46E7-4067-A399-00DA94FE1EBA/Documents/myfile.txt.gz
2009-04-01 11:17:29.335 myApp[1042:20b] Failure To Open Archive

If I manually go into the apps document directory (in os x) where the file is downloaded to I can unpack it without any problems.

Also if I download myfile.txt.gz using safari, place the file in applications documents directory and comment out the createFileAtPath statement it unpacks fine.

What am I doing wrong? Is there any way to get any debuging information out of zipArchive?
skinnytron is offline   Reply With Quote
Old 04-01-2009, 06:02 PM   #19 (permalink)
New Member
 
Join Date: Jul 2008
Posts: 69
Default

Disregard the previous post. That code snippet will work.

The problem I am having is that the file is in the format filename.txt.gz which I'm assuming zipArchive won't extract. Does anyone know of a solution for handling these files in objective-c?
skinnytron is offline   Reply With Quote
Old 04-01-2009, 07:29 PM   #20 (permalink)
jsd
at this moment
 
Join Date: Mar 2009
Location: San Francisco, CA
Posts: 871
Default

Quote:
Originally Posted by skinnytron View Post
Disregard the previous post. That code snippet will work.

The problem I am having is that the file is in the format filename.txt.gz which I'm assuming zipArchive won't extract. Does anyone know of a solution for handling these files in objective-c?
as you have figured out by now, gz is gzip which is different from zip. apparently you can link with libz.dylib on the iPhone but i don't have any sample code showing how to uncompress them.

here's a blog post showing how to compress, maybe you can work backwards from it. How to gzip Data in Memory Using Objective-C — ClintHarris.net
jsd is offline   Reply With Quote
Old 04-02-2009, 01:17 PM   #21 (permalink)
New Member
 
Join Date: Jul 2008
Posts: 69
Default

Here is quick easy solution to unzip a gzip file into memory using zlib.

Code:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
	NSString *sourcePath = [NSString stringWithFormat:@"%@/myFile.txt.gz", [Utility getDocumentsPath]];
	
	NSFileManager *fileManager = [NSFileManager defaultManager];
	[fileManager createFileAtPath:sourcePath contents:data attributes:nil];
	
	gzFile file = gzopen([sourcePath UTF8String], "rb");
	
	unsigned char buffer[CHUNK];
	
	int uncompressedLength = gzread(file, buffer, CHUNK);
	
	NSData *uncompressedData = [NSData dataWithBytes:buffer length:uncompressedLength];
	NSString *txtFile = [[NSString alloc] initWithData:uncompressedData encoding:NSASCIIStringEncoding];

	gzclose(file);
	[fileManager removeItemAtPath:sourcePath error:nil];
}
skinnytron is offline   Reply With Quote
Old 05-22-2009, 02:37 PM   #22 (permalink)
New Member
 
Join Date: May 2009
Posts: 1
Default

Quote:
Originally Posted by indiekiduk View Post
ok thanks removed those files and got it working. Also had to remove the ../../minizip from the includes in ZipArchive.m.
Where did you find the libz.x.x.x.dylid ? I've done everything else, but I think I need to add that library, and don't know where to find it. I'm guessing I have to build it somehow, but I'm not sure how... help is much appreciated!
drummerd is offline   Reply With Quote
Old 07-30-2009, 07:33 PM   #23 (permalink)
New Member
 
Join Date: Jul 2009
Posts: 3
Default cannot use it

Quote:
Originally Posted by drummerd View Post
Where did you find the libz.x.x.x.dylid ? I've done everything else, but I think I need to add that library, and don't know where to find it. I'm guessing I have to build it somehow, but I'm not sure how... help is much appreciated!
I also want to unzip a set of images from the zip file. This zip file is a url. But I can't get to build the above class files in ziparchive link. I will appreciate if somebody puts in a systemeatic way to unzip a zip file with multiple files.
fugusowri is offline   Reply With Quote
Old 07-30-2009, 08:25 PM   #24 (permalink)
Registered Member
 
Join Date: Aug 2008
Posts: 83
Default

Quote:
Originally Posted by drummerd View Post
Where did you find the libz.x.x.x.dylid ? I've done everything else, but I think I need to add that library, and don't know where to find it. I'm guessing I have to build it somehow, but I'm not sure how... help is much appreciated!
Project menu, edit active target, general, click the + under linked libraries. Scroll down.
indiekiduk is offline   Reply With Quote
Old 07-31-2009, 11:17 AM   #25 (permalink)
New Member
 
Join Date: Jul 2009
Posts: 3
Default

Quote:
Originally Posted by indiekiduk View Post
Project menu, edit active target, general, click the + under linked libraries. Scroll down.
I was able to add the library but not bale to compile the wrapper files.
fugusowri is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


» Advertisements
» Stats
Members: 41,860
Threads: 49,768
Posts: 213,052
Top Poster: BrianSlick (3,138)
Welcome to our newest member, gustavo7sexton
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:53 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0