views:

1913

answers:

3

Hi,

I have some resources (zipped) that needs to be shipped with my iphone application. When the app launches for the first time, this zipped file needs to be moved/copied to the Documents folder and unzip it there. User can then add more files to this path from the application. Can someone please suggest how can I achieve this?

Thanks!

+4  A: 

Add the libz.dylib Framework to your project, and include Deusty's NSData gzip category which will give you compression/decompression methods.

Alex Reynolds
Unnecessary to unpack all files to temp storage, if it's possible to uncompress single files (to a file or, preferably, to NSData) on demand (zip archives compress the source files individually and keeps the original folder hierarchy). Don't know of a good iPhone-compatible lib that makes this easy though.
avocade
+2  A: 

While this is available by using the libz.dylib, it really is unnecessary as it save you little (if any) space. You application bundle is already compressed when being transferred to the phone. Compression on top of compression usually yields little additional compression.

Try it out yourself. You may find that shipping your app with unzipped contents may take up just as much space as zipped contents.

coneybeare
The reason I want to add a compressed resource because there are multiple files. If I don't compress then I'll need to move files individually. I'll also need to maintain a list of files somewhere so that I can read the file name and then move them. I thought zipping and unzipping was a simpler solution.
Mithin
You could use `libtar` ( http://www.feep.net/libtar/ ) if you just need a way to archive/unarchive and don't want the time complexity of a `zip`-ped resource.
Alex Reynolds
+6  A: 

Based on your comment above:

The reason I want to add a compressed resource because there are multiple files. If I don't compress then I'll need to move files individually. I'll also need to maintain a list of files somewhere so that I can read the file name and then move them. I thought zipping and unzipping was a simpler solution.

You could add all the files to a folder in your bundle. When the app launches for the first time use fast enumeration to run through the folder and what ever it finds in that folder, it copies into the Documents folder. Handling folders within folders is slightly more complex (add recursion maybe). This way you don't have to worry about zip or tar, nor to keep a directory of files to install.

Just place the folder of files you want into Xcode's resources folder and tell it to import as a folder not as a group. That way the files get installed in your resources inside a folder instead of just as individual files all over the place.

EDIT:

Better yet, do what I say about putting all the files you want in one folder, add to your project, but not as a "Group", and then at first launch use:

[[NSFileManager defaultManager] copyItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error];

and it will copy your whole directory from one place to another. EASY!

mahboudz