 |
|
 |
|
 |
12-10-2008, 02:09 AM
|
#1 (permalink)
|
|
Registered Member
Join Date: Nov 2008
Posts: 35
|
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.
|
|
|
12-28-2008, 08:43 PM
|
#2 (permalink)
|
|
Registered Member
Join Date: Dec 2008
Posts: 429
|
Excellent, thank you so much. How difficult is it to also add functions to unzip objects in memory (eg NSData) ?
|
|
|
12-28-2008, 10:41 PM
|
#3 (permalink)
|
|
Registered Member
Join Date: Nov 2008
Posts: 35
|
Quote:
Originally Posted by lbendlin
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
|
|
|
12-28-2008, 11:22 PM
|
#4 (permalink)
|
|
New Member
Join Date: Aug 2008
Location: Austin, TX
Posts: 239
|
I don't need this at the moment but who knows  , it's great to have in the library. Thanks for the class!
|
|
|
12-28-2008, 11:47 PM
|
#5 (permalink)
|
|
Registered Member
Join Date: Aug 2008
Posts: 80
|
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.
|
|
|
12-29-2008, 01:18 PM
|
#6 (permalink)
|
|
Registered Member
Join Date: Dec 2008
Posts: 429
|
Quote:
Originally Posted by acsolu
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.
|
|
|
12-29-2008, 01:20 PM
|
#7 (permalink)
|
|
Administrator
Join Date: Mar 2008
Location: Richmond, VA
Age: 27
Posts: 745
|
Excellent contribution! You should add this to Google Code, or somewhere similar, so others can contribute to it and have it evolve.
|
|
|
12-29-2008, 01:56 PM
|
#8 (permalink)
|
|
Registered Member
Join Date: Dec 2008
Posts: 429
|
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;
}
|
|
|
01-23-2009, 10:18 AM
|
#9 (permalink)
|
|
Registered Member
Join Date: Nov 2008
Posts: 192
|
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?
|
|
|
01-23-2009, 10:44 AM
|
#10 (permalink)
|
|
Senior Member
Join Date: Jan 2009
Location: Florida, USA
Posts: 471
|
Is all the code free from copyright and patent issues?
|
|
|
01-23-2009, 10:46 AM
|
#11 (permalink)
|
|
Registered Member
Join Date: Dec 2008
Posts: 495
|
Stellar, thanks for sharing that!
|
|
|
01-23-2009, 10:58 AM
|
#12 (permalink)
|
|
Registered Member
Join Date: Aug 2008
Posts: 80
|
bump for jsonli's question. The unzipped data with this class does not respect the zipped data hierarchy - it unzips everything to one path...
|
|
|
01-23-2009, 12:51 PM
|
#13 (permalink)
|
|
Registered Member
Join Date: Nov 2008
Posts: 192
|
Quote:
Originally Posted by ecume
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?
|
|
|
03-09-2009, 09:42 AM
|
#14 (permalink)
|
|
Registered Member
Join Date: Aug 2008
Posts: 83
|
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)
|
|
|
03-09-2009, 05:09 PM
|
#15 (permalink)
|
|
Registered Member
Join Date: Dec 2008
Posts: 429
|
you have to keep the subfolder structure intact. Include only the files that you actually need for your task.
|
|
|
03-09-2009, 08:12 PM
|
#16 (permalink)
|
|
Registered Member
Join Date: Nov 2008
Posts: 35
|
Quote:
Originally Posted by indiekiduk
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
|
|
|
03-16-2009, 10:56 PM
|
#17 (permalink)
|
|
Registered Member
Join Date: Aug 2008
Posts: 83
|
Quote:
Originally Posted by acsolu
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.
|
|
|
04-01-2009, 01:27 PM
|
#18 (permalink)
|
|
New Member
Join Date: Jul 2008
Posts: 69
|
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?
|
|
|
04-01-2009, 06:02 PM
|
#19 (permalink)
|
|
New Member
Join Date: Jul 2008
Posts: 69
|
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?
|
|
|
04-01-2009, 07:29 PM
|
#20 (permalink)
|
|
at this moment
Join Date: Mar 2009
Location: San Francisco, CA
Posts: 871
|
Quote:
Originally Posted by skinnytron
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
|
|
|
04-02-2009, 01:17 PM
|
#21 (permalink)
|
|
New Member
Join Date: Jul 2008
Posts: 69
|
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];
}
|
|
|
05-22-2009, 02:37 PM
|
#22 (permalink)
|
|
New Member
Join Date: May 2009
Posts: 1
|
Quote:
Originally Posted by indiekiduk
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!
|
|
|
07-30-2009, 07:33 PM
|
#23 (permalink)
|
|
New Member
Join Date: Jul 2009
Posts: 3
|
cannot use it
Quote:
Originally Posted by drummerd
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.
|
|
|
07-30-2009, 08:25 PM
|
#24 (permalink)
|
|
Registered Member
Join Date: Aug 2008
Posts: 83
|
Quote:
Originally Posted by drummerd
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.
|
|
|
07-31-2009, 11:17 AM
|
#25 (permalink)
|
|
New Member
Join Date: Jul 2009
Posts: 3
|
Quote:
Originally Posted by indiekiduk
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.
|
|
|
 |
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
» Advertisements |
» Online Users: 386 |
| 29 members and 357 guests |
| AdamSubach, airsoft808, anonymous@, benoitr007, bensj, Danneman, dev123, Duncan C, gtyt38, gustavo7sexton, HemiMG, Jeremy1026, lifeCoder45, maxus182, Ovidius, Paul10, pofak, qilin, Racker, raheel, Sega dude, squidboy, timle8n1, ufbobbo, ultrayard077, ZunePod |
| Most users ever online was 965, 06-30-2010 at 04:26 AM. |
» Stats |
Members: 41,860
Threads: 49,768
Posts: 213,052
Top Poster: BrianSlick (3,138)
|
| Welcome to our newest member, gustavo7sexton |
|