Differentiate file types by file headers (including common file headers)

Source: Internet
Author: User

1. Common file headers (obtained using the ultraedit Text Editor ):

Executable files (such as EXE, bat, DLL, etc.): 4d5a

Office2003 series: 504b

Office2007 series: d0cf

PDF: 2550

2. Code:

 

/// <Summary> /// file type /// </Summary> Public Enum filetype {// <summary> /// enumerate undefined file types /// </Summary> undefined =-1, /// <summary> /// Executable File /// </Summary> pefile = 0, /// <summary> /// office2003 files /// </Summary> office2003 = 1, /// <summary> /// office2007 and 2010 series files /// </Summary> office07and10 = 2, /// <summary> /// PDF file /// </Summary> pdffile = 3, /// <summary> /// DWG file /// </Summary> DW Gfile = 4, /// <summary> // rarfile // </Summary> rarfile = 5, /// <summary> /// ZIP file /// </Summary> zipfile = 6, /// <summary> /// PNG Image /// </Summary> pngfile = 7, /// <summary> /// jpg image /// </Summary> jpgfile = 8} /// <summary> // check the file type /// </Summary> public class filetypecheck {// <summary> // check the file type /// </Summary> /// <Param name = "filepath"> </param> // /<returns> file type enumeration object </returns> pub LIC filetype typecheck (string filepath) {filetype ft; // obtain the ID of the file header, 14 strings string filehead = getfilehead (filepath); // obtain the general length of the file header 4, generally, this length is enough to judge; string normalhead = filehead. substring (0, 4); Switch (normalhead) {Case "4d5a": Ft = filetype. pefile; break; Case "d0cf": Ft = filetype. office2003; break; Case "504b": // 0ffice2007, 2010 is the same as the header of the ZIP file. // The first few bytes must be longer to determine if (filehead = "504b0304140006 ") {ft = Filetype. office07and10;} else // ZIP file {Ft = filetype. zipfile;} break; Case "2550": Ft = filetype. pdffile; break; Case "5261": Ft = filetype. rarfile; break; Case "8950": Ft = filetype. pngfile; break; Case "ffd8": Ft = filetype. jpgfile; break; Case "4143": Ft = filetype. dwgfile; break; default: Ft = filetype. undefined; break;} return ft;} // <summary> // get the file header /// </Summary> /// <Param name = "Filepath"> file path </param> // <returns> Replace the 7-byte array with 14 hexadecimal strings </returns> private string getfilehead (string filepath) {// stores the header information string strheadinfo = string. empty; byte [] buff = new byte [7]; filestream FS = new filestream (filepath, filemode. open); FS. seek (0, seekorigin. begin); FS. read (buff, 0, Buff. length); strheadinfo = bytetohexstr (buff); FS. close (); Return strheadinfo;} // <summary> // convert the byte array to a hexadecimal string /// </Summary> /// <Param name = "bytes"> Read byte array </param> /// <returns> </returns> private string bytetohexstr (byte [] bytes) {string returnstr = ""; if (Bytes! = NULL) {for (INT I = 0; I <bytes. length; I ++) {returnstr + = bytes [I]. tostring ("X2") ;}} return returnstr ;}}

 

 

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.