如何在 Objective C 中查找文件类型
我对此做了很多研究,但没有发现任何有用的东西。我想在 Objective C 中制作一个简单的程序供个人使用,用于打开文件并提供有关它们的信息。我遇到的唯一问题是我找不到正在打开的文件的文件。在不简单地查看扩展名的情况下,有没有办法在 Objective C 中找到文件的完整文件格式?
如果可能的话,我还希望能够以不同的格式保存该文件。有关此主题的信息对于本应用程序也很重要。非常感谢您的帮助。
I have done a lot of research on this and haven't found anything useful. I want am making a simple program in objective c for personal use that opens files and gives information about them. The only problem i have encountered is that i cannot find the file of a file i am opening. Without simply looking at the extension, is there a way to find the full file format of a file in objective c?
If possible, i would also like to be able to save that file in a different format. Information on this subject is also important for this application. Help will be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Mac OS X 的每个文件都附加有类型信息,指定文件的类型。此信息由最后保存文件的应用程序提供,因此不一定正确。此外,新版本的 OS X 会忽略此信息并完全放弃文件扩展名。但是,信息仍然被存储,并且可以使用 NSFileManager 的
attributesOfItemAtPath:error:
方法检索。正如 quixoto 上面提到的,OS X 现在将扩展映射到 UTI。可以使用 NSWorkspace 检索文件的 UTI,它还可以告诉您 UTI 的含义。此代码将在
/full/path/to/file
处获取文件的本地化描述:typeOfFile:error:
方法需要绝对路径。Mac OS X has type information attached to each file which specifies what the type of the file is supposed to be. This information is given by the application which last saved the file, so it is not necessarily correct. Also, new versions of OS X ignore this information and go entirely off of the file extension. However, the information is still stored and can be retrieved using NSFileManager's
attributesOfItemAtPath:error:
method.As mentioned by quixoto above, OS X now maps extensions to UTIs. The UTI of a file can be retrieved using NSWorkspace, which can also tell you what the UTI means. This code will get the localized description of the file at
/full/path/to/file
:The
typeOfFile:error:
method requires an absolute path.考虑一下:“完整文件格式”是什么意思?您关心什么类型的文件和详细程度?
您无法真正像您想要的那样从文件系统中获取“格式”信息。不管你相信与否,文件系统不知道文件的格式。
文件系统传统上将文件存储为具有名称、大小和一些其他属性(“隐藏”、权限等)的字节流。这就是它所负责的一切,应用程序的问题是读取这些字节并将它们解释为有用的东西。
扩展名是向应用程序提供有关文件包含内容的传统提示,但正如您可能猜到的那样,它肯定不是经过验证的保证。
现代 Mac OS X 具有 Quick Look,它使用系统范围的框架,在此基础上添加了一些智能功能,以及扩展和 UTI,这是更丰富的文件类型概念。 文档在这里。我认为没有办法在该系统中查询扩展名和 UTI 之间的映射,但我不确定。
加载一种格式的文件并以另一种格式保存100%取决于您所讨论的文件类型,如果您真的想完成此任务,您将询问有关特定格式的非常具体的问题。 (这个主题远远超出了 Cocoa 中的几个内置方法调用。)
Consider: what do you mean by "full file format"? What kinds of files and level of detail do you care about?
You can't really get "format" information from the file system like you want. Believe it or not, the file system has no idea what format files are in.
File systems traditionally store files as a stream of bytes with a name, a size, and some other attributes ("hidden", permissions, etc). That's all that it's responsible for, and it's the application's problem to read those bytes and interpret them to mean something useful.
The extension is the traditional hint to an application about what a file contains, but as you might guess, it's certainly not a verified guarantee.
Modern Mac OS X has Quick Look, which uses a system-wide framework that adds some smarts on top of this, with mappings between extensions and UTIs, which are a richer notion of file type. Docs are here. I don't think there's a way to query this system for the mappings between extensions and UTIs, but I'm not sure.
Loading a file of one format and saving in another is 100% dependent on the file types you're talking about, and you're going to have ask very specific questions about specific formats if you really care to accomplish this. (And that topic extends well beyond a couple built-in method calls in Cocoa.)
此信息现在(自 OS X 10.5 起)通过 UTI 处理。
你可以这样得到它:
你可以看到 docs 了解有关可以通过这种方式检索的文件属性的更多信息。通过这种方法可以获得相当多的信息。
This information is now (since OS X 10.5) handled through UTIs.
You can get it like this:
You can see the docs for more information about the file properties that can be retrieved this way. There is quite a lot of information available through this method.