Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Libraries to verify file format by header

Tags:

c#

.net

A few times in the last couple months I had the same task of verifying the file format by it's header: JPEG, PDF, Word, and other popular files.

I wonder if there is a library availble for C#/.Net to do that? Or is it a time to start a small project for NuGet catalogue?

like image 762
trailmax Avatar asked Dec 16 '11 17:12

trailmax


People also ask

Why do you think it is a suggested best practice to identify a library's header and source code files using the same name?

It's one of the purposes of header files, to declare things, so you can include it in multiple source files and have the same declaration everywhere the header file is included.

What is a file header?

Definition(s):Data within a file that contains identifying information about the file and possibly metadata with information about the file contents.

HOW include header file in C Linux?

To import a header, use the #include, a preprocessor directive telling the compiler that it should import and process the code before compiling the rest of the code. On a typical C program, it should contain the stdio. h header file, which is the standard header file for input and output streams.

What does a file header look like?

A header file looks like a normal C file, except it ends with . h instead of . c , and instead of the implementations of your functions and the other parts of a program, it holds the declarations.


2 Answers

For those who will find this question in the future: I have started to write the library. Once I have significant amount of different types, I'll submit it to NuGet. But at the moment the source code is available here: http://filetypedetective.codeplex.com/

The idea of the library is to be able to call isPdf() or isZip() on FileInfo objects:

FileInfo file = new FileInfo("C:\Hello.pdf");
if ( file.isPdf())
    Console.WriteLine("File is PDF");

etc.

Update: the finally got around to create nuget package:

Install-Package FileTypeDetective

like image 139
trailmax Avatar answered Oct 04 '22 20:10

trailmax


For most file formats, you can read the magic numbers at the beginning of the file to determine the file type. This is how *nix based systems know the file type regardless of the file extension.

like image 34
John Koerner Avatar answered Oct 04 '22 18:10

John Koerner