开发者

How to obtain a file UTI when the file has no extension?

开发者 https://www.devze.com 2023-03-08 15:18 出处:网络
Is there a way to figure out the file UTI (uniform type identifier) even if there is no file extension?

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消