On Jun 12, 2010, at 7:18 AM, Ben wrote:

> Hi list
> 
> Given an NSData object, with no context as to what it might be, is it 
> possible to find if it contains a document of recognised file type?
> 
> I know this is possible for image files, by using the imageIO framework and 
> doing the following:
> 
> CGImageSourceRef imageSource = 
> CGImageSourceCreateWithData(myNSDataObjectGoesHere, NULL);
> NSString *uti = (NSString *)CGImageSourceGetType(imageSource);
> NSString *extension = [[NSWorkspace sharedWorkspace] 
> preferredFilenameExtensionForType:uti];
> CFRelease(imageSource);
> // Standard typed-in-mail-disclaimer here
> 
> 
> I suppose I could take the long route of writing the data to disk, then using 
> the NSWorkspace method -typeOfFile:error:, but that seems a bit heavy-handed.
> 
> Can anyone suggest a better way of doing this? I've been unable to find 
> anything in the archives or Google, but perhaps I'm using the wrong search 
> terms.
> 
> - Ben

If it’s just for a few types you support, it’s not too hard to check the first 
few bytes to see if they match the magic number of any format you support. For 
example, JPEG files start with 0xff, 0xd8. The magic numbers for most types can 
be found online (Wikipedia is actually a pretty good resource for this a lot of 
the time), or you can sift through the files in /usr/share/file/magic.

Charles_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to