iPhone 自己的数据格式
我想为 iPhone 应用程序创建自己的数据格式。这些文件的结构应与 Apple 的 iWork 文件 (.pages) 类似。这意味着,我有一个包含一些文件的文件夹:
The file 'Juicy.fruit' contains:
Fruits
---> Apple.xml
---> Banana.xml
---> Pear.xml
---> PreviewPicture.png
这个文件夹“Fruits”应该打包在一个方便的文件“Juicy.fruit”中。压缩是没有必要的。我怎样才能做到这一点?我发现了一些开源 ZIP 库。然而,我想用 iPhone 的内置库构建我自己的数据格式(如果可能的话)。
此致, 斯特凡
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我可以通过三种方式阅读您的问题,这是我对每种方式的最佳猜测:
.fruit
文件通过 Safari/SMS/某些网络连接(又名当有人想要下载为您的应用程序制作的文件或由您的应用程序制作的文件时)。在这种情况下,您可以为您的应用程序注册协议,如下所述:
iPhone 文件扩展名应用程序关联
您希望 iPhone 将
.fruit
文件与您的应用程序全局关联,在这种情况下,您需要查看 统一类型标识符。基本上,您在安装程序的info.plst
文件中设置此关联。您想知道如何从一个包含文件的文件夹转变为该文件夹成为带有
.fruit
扩展名的单个文件(包)。如果是这样的话,那么有很多选择,我看不出自己推出自己的选择有什么意义。 Microsoft 和 Adobe 都只是使用标准的 zip 压缩方法并使用自己的扩展名(而不是
.zip
)。如果您将任何 Office 2007 文档(例如docx
或 Adobe 的实验性.pdfxml
文件)放入存档实用程序中(我喜欢 7z,但任何后裔都可以),您将获取一个包含多个 xml 文件的文件夹,就像您所描述的情况一样。 (这也是 Java 的jar
文件类型的工作原理,仅供参考)。因此,除非您有充分的理由避免使用标准压缩方法(我投票 gzip),否则我会遵循这一行业的领先做法。我绝对可以理解在每个可能的级别上进行 DIY 的冲动,但您基本上是在问(如果是#3)如何创建自己的打包算法,并且在阅读了一些最基本的压缩方法的工作原理之后,我会别管那个人了。另外,我真的怀疑苹果是否内置了库来完成大多数人只会使用标准方法的事情。
最后一点:
如果您真的想从头开始(仍然建议不要),因为您的文件都是 XML,您可以创建一个新的 XML 文件,该文件将充当某种包装器,并将每个文件放入那个包装文件。但是,当需要解开包装时,这确实是多余的,因为每次都必须加载整个文件。但它会是这样的:
但有了这个想法,你要么必须选择解压它,从而冒着失去文件跟踪、覆盖一些但不是全部等等的风险,要么你总是把它当作一个大文件,在在这种情况下,与存档不同,您每次都必须加载所有数据才能提取任何内容,而不是仅从存档中提取所需的文件。
但如果你有决心的话,它可能会起作用。
另外,如果您有兴趣,有一个专门用于移动 XML 的传输协议,称为 WBXML(Wap 二进制 XML)。不确定它是否仍然被认真对待,但如果有一个 iPhone 库,你应该研究一下。
Okay, so there are three ways I am reading your question, here's my best guess on each one:
.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
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'sinfo.plst
file.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 asdocx
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'sjar
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:
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.