Originally Posted by Krystian Galaj
Many programs recognize file type based on content. Most file formats have fixed headers, easy to recognize. There could even be a GPL library for that lying around.
Unix units have a file command that makes the work. It has a magic database to supplement the built in header data and can be extended by the user. Most Unix programs use the file utility to perform this function. On other platforms this functionality may or may not exist.