Python examples of file types judged by file headers

Source: Internet
Author: User
Tags rar zip


For the server to provide upload, need to upload the file to filter, otherwise various Webshell, Bauku.


Import struct

# Support File types
# The purpose of using a 16 string is to know how many bytes the file header is
# Various file headers are not the same length, less than 2 characters long, 8 characters
Def typelist ():
return {
"52617221": Ext_rar,
"504b0304": Ext_zip}

# byte code ext. 16 binary string
def bytes2hex (bytes):
num = Len (bytes)
hexstr = u ""
For I in range (num):
t = u "%x"% bytes[i]
If Len (t)% 2:
hexstr = u "0"
Hexstr = t
Return Hexstr.upper ()

# Get File type
def filetype (filename):
Binfile = open (filename, ' RB ') # required binary word read
TL = typelist ()
Ftype = ' Unknown '
For Hcode in Tl.keys ():
Numofbytes = Len (hcode)/2 # How many bytes do I need to read?
Binfile.seek (0) # Every time you read it, go back to the file header, or you'll read it backwards.
Hbytes = Struct.unpack_from ("b" *numofbytes, Binfile.read (numofbytes)) # a "B" denotes a byte
F_hcode = Bytes2hex (hbytes)
if F_hcode = = Hcode:
Ftype = Tl[hcode]
Break
Binfile.close ()
Return ftype

if __name__ = = ' __main__ ':
Print filetype (your-file-path)

Example


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 ("File format is rar." + result);

}

Else

{

Console.WriteLine ("File format is not rar." + result);

}

Console.ReadLine ();

}

}

}

File headers for common file formats


File format file header (hexadecimal)

JPEG (jpg) ffd8ff
PNG (PNG) 89504E47
gif (GIF) 47494638
TIFF (TIF) 49492a00
Windows Bitmap (BMP) 424d CAD (DWG) 41433130
Adobe Photoshop (PSD) 38425053
Rich Text Format (RTF) 7b5c727466
XML (XML) 3C3F786D6C
HTML (HTML) 68746d6c3e
Email [thorough only] (EML) 44656c69766572792d646174653a
Outlook Express (dbx) cfad12fec5 fd746f
Outlook (PST) 2142444E
MS word/excel (xls.or.doc) d0cf11e0
Ms Access (MDB) 5374616e64617264204a
Wor Dperfect (WPD) FF575043
Postscript (eps.or.ps) 252150532d41646f6265
Adobe Acrobat (pdf) 255044462d312e
Quicken (QDF) ac9ebd8f
Windows Password (PWL) E3828596
ZIP Archive (ZIP) 504b0304
rar Archive (RAR) 52617221 Wave (WAV) 57415645
avi (AVI) 41564920
Real Audio (RAM) 2E7261FD
Real Media (RM) 2e524d46
MPEG (MPG) 00 0001BA
MPEG (MPG) 000001b3
Quicktime (mov) 6d6f6f76
Windows Media (ASF) 3026B2758E66CF11
MIDI (mid) 4d5468

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.