常见文件类型识别

来源:互联网 发布:fx组合red light知乎 编辑:程序博客网 时间:2024/04/29 06:08
根据文件的后缀名识别文件类型并不准确,可以使用文件的头信息进行识别: 

以下是各类文件的头: 

FFD8FFE1=JPEG(jpg)
FFD8FF=JPEG(jpg)
89504E47=PNG(png)
89504E=PNG(png)
47494638=GIF(gif)
474946=GIF(gif)
49492A00=TIFF(tif)
424D46=Windows\ Bitmap(bmp)
424D=Windows\ Bitmap(bmp)
41433130=CAD(dwg)
38425053=Adobe\ Photoshop(psd)
7B5C727466=Rich\ Text\ Format(rtf)
3C3F786D6C=XML(xml)
68746D6C3E=HTML(html)
44656C69766572792D646174653A=Email\ [thorough\ only](eml)
44656C69766572792D646174=Email\ [thorough\ only](eml)
CFAD12FEC5FD746F=Outlook\ Express(dbx)
2142444E=Outlook(pst)

D0CF11E0=MS\ Word/Excel(xls.or.doc)
5374616E64617264204A=MS\ Access(mdb)
FF575043=WordPerfect(wpd)
252150532D41646F6265=Postscript(eps.or.ps)
255044462D312E=Adobe\ Acrobat(pdf)
255044=Adobe\ Acrobat(pdf)
AC9EBD8F=Quicken(qdf)
E3828596=Windows\ Password(pwl)
504B0304=ZIP\ Archive(zip)
504B03=ZIP\ Archive(zip)
52617221=RAR\ Archive(rar)
526172=RAR\ Archive(rar)
57415645=Wave(wav)
41564920=AVI(avi)
2E7261FD=Real\ Audio(ram)
2E524D46=Real\ Media(rm)
000001BA=MPEG(mpg)
000001B3=MPEG(mpg)
6D6F6F76=Quicktime(mov)
3026B2758E66CF11=Windows\ Media(asf)
4D546864=MIDI(mid)
###########################
0xFFD8FF=JPEG(jpg;jpeg)
0x89504E470D0A1A0A=PNG(png)
GIF8=GIF(gif)
0x49492A00=TIFF(tif;tiff)
0x4D4D002A=TIFF(tif;tiff)
BM=Bit\ map(bmp)
0x4A47040E000000=AOL\ ART(art)
0x4A47030E000000=AOL\ ART(art)
0x0A050108=PC\ Paintbrush(pcx)
0xD7CDC69A=Graphics\ Metafile(wmf)
0x01000900=Graphics\ Metafile(wmf)
0x02000900=Graphics\ Metafile(wmf)
0x0100000058000000=Enhanced\ Metafile(emf)
CDR=Corel\ Draw(cdr)
0x41433130=CAD(dwg)
8BPS=Adobe\ Photoshop(psd)
rtf=Rich\ Text\ Format(rtf)
HTML(html;htm;php;php3;php4;phtml;shtml)type
Delivery-date:=Email(eml)
0xCFAD12FE=Outlook\ Express(dbx)
0xD0CF11E0A1B11AE1=MS\ Office/OLE2(doc;xls;dot;ppt;xla;ppa;pps;pot;msi;sdw;db)
Standard J=MS\ Access(mdb;mda;mde;mdt)
0xFF575043=WordPerfect(wpd)
writer=OpenOffice\ Writer(sxw)
calc=OpenOffice\ Calc(sxc)
math=OpenOffice\ Math(sxm)
impress=OpenOffice\ Impress(sxi)
draw=OpenOffice\ Draw(sxd)
<MAKERFILE=Adobe\ FrameMaker(fm)
%!PS-Adobe=PostScript(eps.or.ps;ps;eps)
%PDF-1.=Adobe\ Acrobat(pdf)
0xAC9EBD8F=Quicken(qdf)
0x458600000600=QuickBooks\ Backup(qbb)
0x53520100=Sage(sly.or.srt.or.slt;sly;srt;slt)
SAGEBACKUP=Sage\ Backup(1)
0x576F726450726F=Lotus\ WordPro\ v9(lwp)
0x00001A00051004=Lotus\ 123\ v9(123)
0x00001A0002100400=Lotus\ 123\ v5(wk4)
0x00001A0000100400=Lotus\ 123\ v3(wk3)
0x2000604060=Lotus\ 123\ v1(wk1)
0xE3828596=Windows\ Password(pwl)
0x504B0304=ZIP\ Archive(zip;jar)
0x504B3030=ZIP\ Archive(outdated)(zip)
0x5F27A889=Jar\ Archive
Rar!=RAR\ Archive(rar)
0x1F8B08=GZ\ Archive(gz;tgz)
BZh=BZIP\ Archive(bz2)
0x60EA=ARJ\ Archive(arj)
7z\u96c6'=7\uff0dZIP\ Archive(7z)
WAVE=Wave(wav)
AVI=AVI(avi)
.ra?0=Real\ Audio(ram;ra)
.RMF=Real\ Media(rm)
0x000001BA=MPEG(mpg;mpeg)
0x000001B3=MPEG(mpg;mpeg)
moov=Quicktime(mov)
0x3026B2758E66CF11=Windows\ Media(asf)
<\?wpl=MediaPlayer\ Playlist
MThd=MIDI(mid)
MZ=Win32\ Executable(exe;dll;drv;vxd;sys;ocx;vbx)
MZ=Win16\ Executable(exe;dll;drv;vxd;sys;ocx;vbx)
0x7F454C4601010100=ELF\ Executable(elf;;)
494433=MP3
FFFD74=MP3
FFFA51=MP3
FFFA91=MP3
FFFA71=MP3
FFFA61=MP3
FFFDB0=MP3
FFFB90=MP3
FFFB92=MP3
FFFD74=MP3
FFF368=MP3
FFF374=MP3
FFF340=MP3
EFBBBF=Text(txt)
3F5F0300=Windows_Help
4C4E0200=Windows_Help
MZ.00-02=exe

使用方法如下:

import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.util.HashMap;import java.util.Map;public class FileTypeDetector {private static Map<String,String> head2FileType = new HashMap<String,String>();static{head2FileType.put("FFD8FFE1", "jpg");head2FileType.put("89504E47", "png");head2FileType.put("47494638 ", "gif");head2FileType.put("49492A00", "tif");head2FileType.put("424D", "bmp");head2FileType.put("41433130", "dwg");head2FileType.put("38425053 ", "psd");head2FileType.put("7B5C727466", "rtf");head2FileType.put("3C3F786D6C", "xml");head2FileType.put("68746D6C3E ", "html");head2FileType.put("44656C69766572792D646174", "eml");head2FileType.put("CFAD12FEC5FD746F ", "dbx");head2FileType.put("2142444E", "pst");head2FileType.put("D0CF11E0", "xls/doc");head2FileType.put("5374616E64617264204A", "mdb");head2FileType.put("FF575043", "wpd");head2FileType.put("252150532D41646F6265", "eps/ps");head2FileType.put("255044462D312E", "pdf");head2FileType.put("E3828596", "pwl");head2FileType.put("504B0304", "zip");head2FileType.put("52617221", "rar");head2FileType.put("57415645", "wav");head2FileType.put("41564920", "avi");head2FileType.put("2E7261FD", "ram");head2FileType.put("2E524D46", "rm");head2FileType.put("000001BA", "mpg");head2FileType.put("000001B3", "mpg");head2FileType.put("6D6F6F76", "mov");head2FileType.put("3026B2758E66CF11", "asf");head2FileType.put("4D546864", "mid");}private static String bytesToHexString(String fileName) throws IOException{        FileInputStream fis = null;        StringBuilder stringBuilder = new StringBuilder();        try{        fis = new FileInputStream(new File(fileName));        byte[] b = new byte[4];        fis.read(b, 0, b.length);        for (int i = 0; i < b.length; i++) {            int v = b[i] & 0xFF;            String hv = Integer.toHexString(v);            if (hv.length() < 2) {                stringBuilder.append(0);            }            stringBuilder.append(hv);        }        }finally{        if(fis != null)        fis.close();        }        return stringBuilder.toString().toUpperCase();    }public static String fileType(String fileName) throws IOException{String head = bytesToHexString(fileName);return head2FileType.get(head);}public static void main(String[] args) throws IOException {System.out.println(fileType("d://aaa.png"));}}

原文地址:http://fuliang.iteye.com/blog/769680

原创粉丝点击