Friday, April 20, 2001 07:46 AM
Consider following 2 solutions:
- By the file name extension
- Opening the file and looking for the "magic number"
The first is very easy but not sure at 100%.
The "magic number" is a little sequence of bytes inside binary files that identifies the file's type.
Programs that handle those file types always try to check if the file is of correct type, and contains correct data, checking "magic number" at first.
For example for Java
.class files the magic number is located as the first four bytes is 0xCAFEBABE hex (decimal: 3405691582). For
.zip the first four bytes are "PK" followed by 0x03 then 0x04 (this is 0x504B0304 hex).
And so on...
You can open different kind of compressed files and identify a constant byte pattern that shall be the "magic number" of the file type.
The best way is to use both solutions together.