0

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

Stefan
  • 28,843
  • 15
  • 64
  • 76
  • I'll admit that I don't know much about non-web applications, but I still have to ask-- If the packaged files (the `.fruit`) files are only going to work on apps that you create, then why do you need to build a data format? Shouldn't the app just know to look for the file type and handle it as needed? Or are you wanting to register the file type globally for other apps or so Safari knows to use your app when someone downloads a `.fruit` file? Again, I'm honestly confused, not telling you how things work. – Anthony Mar 17 '10 at 14:56
  • Real quick, I started researching your question and I found an interesting alternative. You can register protocols with your app so that safari associates the protocol with you app. So you could register `fruit://` and any links starting with that will open your app. Check out: http://stackoverflow.com/questions/1286615/iphone-file-extension-app-association – Anthony Mar 17 '10 at 15:01
  • The crucial point of my project is to package several files into one file in order to exchange these bundles easily over the network. Registering the file type globally would be a nice side effect, but is not necessary. – Stefan Mar 17 '10 at 15:03
  • Anthony, thank you for the link. This would be a nice addition to my original plan. :-) – Stefan Mar 17 '10 at 15:04

1 Answers1

3

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:

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.

Community
  • 1
  • 1
Anthony
  • 36,459
  • 25
  • 97
  • 163
  • 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 Mar 17 '10 at 15:50