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.
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.
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;
}
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
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.
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?
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?
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.
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!
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.
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.