Dr.Kameleon
Dr.Kameleon

Reputation: 22810

Get file type of given file - based on contents

OK, it may sound fairly straightforward but I'm still not sure how to go about it.

I know it's possible to check file type based on file extensions, using UTIs (e.g. Get the type of a file in Cocoa).

However, I need to be able to get the file type (in more general terms, like "text", "image", "else"), depending on the content.

Is that possible?

Any ideas?

Upvotes: 2

Views: 74

Answers (1)

trojanfoe
trojanfoe

Reputation: 122381

One route forward is to call the file command and parse its output, but that is fairly horrible, and I wouldn't do that as it's slow and you are susceptible to changes in the output.

The file command uses a pretty extensive database of byte patterns to test the contents of the file and I would be tempted to implement my own internal version of it, or use this library (which I think might need some work before it works under OSX).

Upvotes: 2

Related Questions