简体   繁体   中英

How to obtain a file UTI when the file has no 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM