views:

47

answers:

1

Hi,

I would like to create my own data format for an iPhone app. The files should be similar structured as e.g. Apple's iWork files (.pages). That means, I have a folder with some files in it:

The file 'Juicy.fruit' contains:
   Fruits
   ---> Apple.xml
   ---> Banana.xml
   ---> Pear.xml
   ---> PreviewPicture.png

This folder "Fruits" should be packed in a handy file 'Juicy.fruit'. Compression isn't necessary. How could I achieve this? I've discovered some open source ZIP-libraries. However, I would like to to build my own data format with the iPhones built-in libs (if possible).

Best regards, Stefan

+2  A: 

Okay, so there are three ways I am reading your question, here's my best guess on each one:

  1. You want your .fruit files to be associated with your app via Safari/SMS/some network connection (aka when someone wants to download files made for your app or made by your app).

In this case, you can register a protocol for your app, as discussed here:

http://stackoverflow.com/questions/1286615/iphone-file-extension-app-association

  1. You want the iPhone to globally associate .fruit files with your app, in which case you want to look into Uniform Type Identifiers. Basically, you set up this association in your installer's info.plst file.

  2. You want to know how you can go from having a folder with files in it to that folder being a single file (package) with your .fruit extension.

If that's the case, there are many options out there and I don't see a purpose in rolling your own. Both Microsoft and Adobe simply use a standard zip compression method and use their own extension (instead of .zip). If you drop any office 2007 document, such as docx or Adobe's experimental .pdfxml file into an archive utility (I like 7z, but any descent one will do), you will get a folder with several xml files, just like you're describing for your situation. (This is also how Java's jar file type works, fyi). So unless you have a great reason to avoid standard compression methods (I vote gzip), I would follow the industry lead on this one.

I can definitly appreciate the urge to go DIY at every level possible, but you're basically asking (if it's #3) how you can create your own packaging algorithm, and after reading how some of the most basic compression methods work, I would leave that one alone. Plus I really doubt that Apple has built in libraries for doing something that most people will just use standard methods for.

One last note:

If you are really gunning to do it from scratch (still suggest not), since your files are all XML, you could just create a new XML file that will act as a wrapper of sorts, and have each file go into that wrapper file. But this would be really redundant when it came time to unwrap, as it would have to load the whole file every time. But it would be something like:

Juicy.fruit --
   <fruit-wrapper>
       <fruit>
          <apple>
              ... content from apple.xml
          </apple>
       </fruit>
       <fruit>
          <banana>
              ... content from banana.xml
          </banana>
       </fruit>
       <fruit>
          <pear>
              ... content from pear.xml
          </pear>
       </fruit>
       <picture>
              ...URL-encoded binary of preview picture
       </picture>
   </fruit-wrapper>     

But with this idea, you either have to choose to unpack it, and thus risk losing track of the files, overwriting some but not all, etc etc, or you always treat it like one big file, in which case, unlike with archives, you have to load all of the data each time to pull anything out, instead of just pulling the file you want from the archive.

But it could work, if you're determined.

Also, if you are interested, there is a transfer protocol intended specifically for XML over mobile called WBXML (Wap Binary XML). Not sure if it is still taken seriously, but if there is an iPhone library for it, you should research it.

Anthony
Thank you very much for your ideas. I like your XML approach. If this won't work with my file sizes, I will use gzip (for which I've found an interesting code snippets a few moments ago: http://www.mobileapps.de/2008/09/14/working-with-gzip-compressed-files-on-iphone/)
Stefan