当文件没有扩展名时如何获取文件UTI?

发布于 2024-11-09 17:46:11 字数 536 浏览 0 评论 0原文

即使没有文件扩展名,有没有办法找出文件 UTI(统一类型标识符)?

现在,当文件具有扩展名时,我会像这样获得它:

CFStringRef fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (CFStringRef)[file pathExtension], NULL);

我在网上找到了这个片段,旨在在 Mac 上执行此操作。但我在iOS中:

CFStringRef typeString = UTCreateStringForOSType(outInfo.filetype);
itemUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, typeString, NULL);
CFRelease( typeString );

注意:outInfo是LSItemInfoRecord类型,iOS中不存在。

Is there a way to figure out the file UTI (uniform type identifier) even if there is no file extension?

Right now I obtain it like this, when the file has an extension:

CFStringRef fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (CFStringRef)[file pathExtension], NULL);

I've found this snippet on the net which aims to do it on the Mac. But I am in iOS:

CFStringRef typeString = UTCreateStringForOSType(outInfo.filetype);
itemUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, typeString, NULL);
CFRelease( typeString );

Note: outInfo is of type LSItemInfoRecord which does not exist in iOS.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情未る 2024-11-16 17:46:11

识别没有扩展名且没有其他元数据的文件的唯一方法是查看其内容并据此识别该文件。

如果它是一个图像文件,那么它通常以一些神奇的字符串开头,将其标识为 JPEG、PNG 等。您也可以尝试将其作为图像打开,看看它是否有效。

但对于文本文件,该方法不起作用,因为文本文件可以包含任何内容。如果您对文件中的文本有所了解,则可以将其作为字符串打开,然后在字符串中搜索该文本。如果没有,您可以只从字典中查找单词,然后如果它包含合理数量的字典单词,则假设它是文本。

如果没有扩展名,您可能最好只是要求用户识别文件类型 - 这更容易、更可靠。

The only way to identify a file that has no extension and no other metadata is to look at its contents and identify the file based on that.

If it's an image file then it usually starts with some magic string that identifies it as a JPEG, PNG, etc. You could also just try to open it as an image and see if it works.

For a text file though, that approach won't work as text files can contain any content. If you have some idea about what text is in the file you could just open it as a string and then search the string for that text. If not, you could just look for words from a dictionary and then assume that it's text if it contains a reasonable number of dictionary words.

You might be better off just asking the user to identify the file type if there's no extension though - it's easier and more reliable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文