Determine the file type using the file header

Source: Internet
Author: User

When uploading files, you often need to determine the file type, such as examples and documents. The common practice is to directly determine the file suffix, in order to prevent various attacks, literary and art youths also use the file header information to determine the file type.

The principle is simple: you can use the file header to directly read the first two bytes of the file.

Demo

Using system;

Using system. IO;

 

Namespace helloworld

{

Class Program

{

Static void main (string [] ARGs)

{

Filestream FS = new filestream (@ "C: \ test.rar", filemode. Open, fileaccess. Read );

Binaryreader reader = new binaryreader (FS); // convert to binary stream

Byte [] buff = new byte [2];

String result = string. empty;

Try

{

FS. Read (buff, 0, 2 );

Result = buff [0]. tostring () + buff [1]. tostring ();

}

Catch (exception ex)

{

Throw ex;

}

 

Reader. Close ();

FS. Close ();

 

If (result = "8297") // RAR: 8297

{

Console. writeline ("the file format is RAR." + result );

}

Else

{

Console. writeline ("the file format is not RAR." + result );

}

Console. Readline ();

}

}

}

 

Attachment of common file header information

Common File Header (decimal)

. Jpg: 255,216

GIF: 71,73

BMP: 66,77

PNG: 137,80

DOC: 208,207.

Docx: 80, 75

XLS: 208,207

XLSX: 80, 75

JS: 239,187

SWF: 67,87

TXT: 70, 67

MP3: 73,68

WMA: 48, 38

Mid: 77,84

RAR: 82,97

ZIP: 80, 75

XML: 60, 63

The file headers of common files are as follows (hexadecimal ):

JPEG (JPG), file header: ffd8ff

PNG (PNG), file header: 89504e47

GIF, file header: 47494638

Tiff (TIF), file header: 492.16a00

Windows Bitmap (BMP), file header: mongod

CAD (DWG), file header: 41433130

Adobe Photoshop (PSD), file header: 38425053

Rich Text Format (RTF), file header: 7b5c727466

XML (XML), file header: 3c3f786d6c

HTML (HTML), file header: 68746d6c3e

Email [thorough only] (EML), file header: 44656c69766572792d646174243a

Outlook Express (DBX), file header: cfad12fec5fd746f

Outlook (PST), file header: 2142444e

MS word/Excel (xls.or.doc), file header: d0cf11e0

MS access (MDB), file header: 5374616e64617213204a

Wordperfect (WPD), file header: ff575043

Postscript (EPS. Or. ps), file header: 252150532d41646f6265

Adobe Acrobat (PDF), file header: 255044462d312e

Quicken (qdf), file header: ac9ebd8f

Windows Password (pwl), file header: e3828596

ZIP Archive (ZIP), file header: 504b0304

RAR Archive (RAR), file header: 52617221

Wave (WAV), file header: 57415645

AVI (AVI), file header: 41564920

Real Audio (RAM), file header: 2e00001fd

Real Media (RM), file header: 2e524d46

MPEG (MPG), file header: 000001ba

MPEG (MPG), file header: 000001b3

QuickTime (mov), file header: 6d6f6f76

Windows Media (ASF), file header: 3026b2758e66cf11

MIDI (MID), file header: 4d546864

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.