tags:

views:

960

answers:

3

Goal: download a zipped file, unzip it, and save it in the iPhone app's Documents directory.

The following code makes use of the initWithGzippedData method that was added to NSData in the Molecule app found here: http://www.sunsetlakesoftware.com/molecules

As adapted to my app:

NSString *sFolder = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *sFileName = [sFolder stringByAppendingPathComponent:@"MyFile.db"];
NSURL *oURL = [NSURL URLWithString: @"http://www.isystant.com/Files/MyFile.zip"];
NSData *oZipData = [NSData dataWithContentsOfURL: oURL];
NSData *oData = [[NSData alloc] initWithGzippedData:oZipData];
[oZipData release];
b = [oData writeToFile:sFileName atomically:NO];
NSLog(@"Unzip %i", b);

Result: A zip file is successfully downloaded. From it a new, supposedly unzipped file is created in the Documents directory with the desired name (MyFile.db) but it has zero bytes.

Anybody see the problem? Or else is there a simpler way to unzip a downloaded file than the one used in the Molecules app?

+1  A: 

I think that your problem may be that you are attempting to gzip-deflate a Zip file. Those are two different compression algorithms.

I based the gzip-deflating code in Molecules on this NSData category provided by the contributors to the CocoaDev wiki. What you'll want to do is use their -zlibDeflate implementation, which should properly unzip a Zip file.

Unrelated to your problem, instead of using NSHomeDirectory() and appending a path component, the recommended approach for finding the documents directory is the following:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
Brad Larson
Thank you, I was beginning to suspect that gzip and zip are different. I will always use my Mac to zip the file that my app downloads from my server, so that means I need zlibDeflate, as you say.Regarding the path, where can I read the recommendation that you mention? I'm always eager to use best practices.
Scott Pendleton
@Scott Pendleton: This might be what you're looking for, documentation-wise: http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/FilesandNetworking/FilesandNetworking.html#//apple_ref/doc/uid/TP40007072-CH21-SW6 In general, I think you want to use these helper methods instead of hardcoding path components, just in case that component changes at some point in the future (for internationalization reasons, perhaps).
Brad Larson
A: 

NSString *saveLocation = @"/Volumes/Extra/samples/unzipTest/song.zip";

NSData *zipData=[[NSData alloc] initWithContentsOfFile:saveLocation];

bool b= [[zipData zlibDeflate] writeToFile:@"/Users/admin/Desktop/untitled folder" atomically:YES];

i am doing this but it failing to unzip where i am wrong.

jeeva
A: 

You should make sure your file is never too big, as you are loading it fully into memory before the unzip starts.

buggles