Jump to content
Sign in to follow this  
eaolson

Determining file type in a platform-independent way

Recommended Posts

Since I'm a practitioner of the Dark Arts, I don't really work with Macs, just Windows. My last significant experience on a Mac was with OS 9, so I know very little about OS X. I know that pre-OS-X, Macs used to store the file type and other metadata information separate from the file itself, so the the file name was no indication of what type a file was (i.e., no file extension). Is that true on OS X, or does it use the file extension?

I guess my larger question is whether or not there is a platform-independent way to determine a file type (png, doc, etc.)? Or does everyone in practice just use the file extension?

Share this post


Link to post
Share on other sites

In general any sophisticated OS should not use the filename to distinguish what "type" a file is.

However, Windows tends to rely on this quite a lot. Many file formats have a certain header at the beginning of the file, regardless of which file extension it has.

If you have some way of identifying an "unknown" file, then this is the way to do it. If it's an unknown file with unknown format, then I guess you're out of luck. You can relatively easily distinguish a PNG file for example.

From Wikipedia: "A PNG file starts with an 8-byte signature. The hexadecimal byte values are 89 50 4E 47 0D 0A 1A 0A. Each of the header bytes is there for a specific reason:"

Many MSOffice documents also have a certain header for example, but renaming an XLS file to something else breaks it's connection to the application. Windoh!s.

Since reading the file contents is quite a time-consuming matter, the file extension (even though it's really only an educated guess) is mostly used.

Shane.

QUOTE(eaolson @ Aug 12 2007, 05:54 PM)

Since I'm a practitioner of the Dark Arts, I don't really work with Macs, just Windows. My last significant experience on a Mac was with OS 9, so I know very little about OS X. I know that pre-OS-X, Macs used to store the file type and other metadata information separate from the file itself, so the the file name was no indication of what type a file was (i.e., no file extension). Is that true on OS X, or does it use the file extension?

I guess my larger question is whether or not there is a platform-independent way to determine a file type (png, doc, etc.)? Or does everyone in practice just use the file extension?

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.