VerifyFileExtensionName.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
  2. using System.Xml;
  3. using System.Xml.Linq;
  4. using System.Xml.Serialization;
  5. namespace Admin.NET.Core;
  6. public static class VerifyFileExtensionName
  7. {
  8. private static IDictionary<string, string> dics_ext = new Dictionary<string, string>();
  9. private static IDictionary<string, HashSet<int>> ext_dics = new Dictionary<string, HashSet<int>>();
  10. static VerifyFileExtensionName()
  11. {
  12. dics_ext.Add("FFD8FFE0", ".jpg");
  13. dics_ext.Add("89504E47", ".png");
  14. dics_ext.Add("47494638", ".gif");
  15. dics_ext.Add("49492A00", ".tif");
  16. dics_ext.Add("424D", ".bmp");
  17. //PS和CAD
  18. dics_ext.Add("38425053", ".psd");
  19. dics_ext.Add("41433130", ".dwg"); // CAD
  20. dics_ext.Add("252150532D41646F6265", ".ps");
  21. //办公文档类
  22. dics_ext.Add("D0CF11E0", ".doc"); //ppt、doc、xls
  23. dics_ext.Add("504B0304", ".docx");//pptx、docx、xlsx
  24. /**注意由于文本文档录入内容过多,则读取文件头时较为多变-START**/
  25. dics_ext.Add("0D0A0D0A", ".txt");//txt
  26. dics_ext.Add("0D0A2D2D", ".txt");//txt
  27. dics_ext.Add("0D0AB4B4", ".txt");//txt
  28. dics_ext.Add("B4B4BDA8", ".txt");//文件头部为汉字
  29. dics_ext.Add("73646673", ".txt");//txt,文件头部为英文字母
  30. dics_ext.Add("32323232", ".txt");//txt,文件头部内容为数字
  31. dics_ext.Add("0D0A09B4", ".txt");//txt,文件头部内容为数字
  32. dics_ext.Add("3132330D", ".txt");//txt,文件头部内容为数字
  33. /**注意由于文本文档录入内容过多,则读取文件头时较为多变-END**/
  34. dics_ext.Add("7B5C727466", ".rtf"); // 日记本
  35. dics_ext.Add("255044462D312E", ".pdf");
  36. //视频或音频类
  37. dics_ext.Add("3026B275", ".wma");
  38. dics_ext.Add("57415645", ".wav");
  39. dics_ext.Add("41564920", ".avi");
  40. dics_ext.Add("4D546864", ".mid");
  41. dics_ext.Add("2E524D46", ".rm");
  42. dics_ext.Add("000001BA", ".mpg");
  43. dics_ext.Add("000001B3", ".mpg");
  44. dics_ext.Add("6D6F6F76", ".mov");
  45. dics_ext.Add("3026B2758E66CF11", ".asf");
  46. //压缩包
  47. dics_ext.Add("52617221", ".rar");
  48. dics_ext.Add("504B03040A000000", ".zip");
  49. dics_ext.Add("1F8B08", ".gz");
  50. //程序文件
  51. dics_ext.Add("3C3F786D6C", ".xml");
  52. dics_ext.Add("68746D6C3E", ".html");
  53. //dics_ext.Add("7061636B", ".java");
  54. //dics_ext.Add("3C254020", ".jsp");
  55. //dics_ext.Add("4D5A9000", ".exe");
  56. dics_ext.Add("44656C69766572792D646174653A", ".eml"); // 邮件
  57. dics_ext.Add("5374616E64617264204A", ".mdb");//Access数据库文件
  58. dics_ext.Add("46726F6D", ".mht");
  59. dics_ext.Add("4D494D45", ".mhtml");
  60. //
  61. foreach (var dics in dics_ext)
  62. {
  63. if (!ext_dics.ContainsKey(dics.Value))
  64. ext_dics.Add(dics.Value, new HashSet<int> { dics.Key.Length / 2 });
  65. else
  66. ext_dics[dics.Value].Add(dics.Key.Length / 2);
  67. }
  68. }
  69. /// <summary>
  70. /// 文件格式和文件内容格式是否一致
  71. /// </summary>
  72. /// <param name="stream"></param>
  73. /// <param name="suffix"></param>
  74. /// <returns></returns>
  75. public static bool IsSameType(Stream stream, string suffix = ".jpg")
  76. {
  77. if (stream == null)
  78. return false;
  79. //
  80. suffix = suffix.ToLower();
  81. if (!ext_dics.ContainsKey(suffix))
  82. return false;
  83. try
  84. {
  85. foreach (var Len in ext_dics[suffix])
  86. {
  87. byte[] b = new byte[Len];
  88. stream.Read(b, 0, b.Length);
  89. //string fileType = System.Text.Encoding.UTF8.GetString(b);
  90. string fileKey = GetFileHeader(b);
  91. if (dics_ext.ContainsKey(fileKey))
  92. return true;
  93. }
  94. }
  95. catch (IOException e)
  96. {
  97. }
  98. return false;
  99. }
  100. /**
  101. * 根据文件转换成的字节数组获取文件头信息
  102. *
  103. * @param filePath
  104. * 文件路径
  105. * @return 文件头信息
  106. */
  107. private static string GetFileHeader(byte[] b)
  108. {
  109. string value = BytesToHexString(b);
  110. return value;
  111. }
  112. /**
  113. * 将要读取文件头信息的文件的byte数组转换成string类型表示
  114. * 下面这段代码就是用来对文件类型作验证的方法,
  115. * 将字节数组的前四位转换成16进制字符串,并且转换的时候,要先和0xFF做一次与运算。
  116. * 这是因为,整个文件流的字节数组中,有很多是负数,进行了与运算后,可以将前面的符号位都去掉,
  117. * 这样转换成的16进制字符串最多保留两位,如果是正数又小于10,那么转换后只有一位,
  118. * 需要在前面补0,这样做的目的是方便比较,取完前四位这个循环就可以终止了
  119. * @param src要读取文件头信息的文件的byte数组
  120. * @return 文件头信息
  121. */
  122. private static string BytesToHexString(byte[] src)
  123. {
  124. StringBuilder builder = new StringBuilder();
  125. if (src == null || src.Length <= 0)
  126. {
  127. return null;
  128. }
  129. string hv;
  130. for (int i = 0; i < src.Length; i++)
  131. {
  132. // 以十六进制(基数 16)无符号整数形式返回一个整数参数的字符串表示形式,并转换为大写
  133. hv = Convert.ToString(src[i] & 0xFF, 16).ToUpper();
  134. if (hv.Length < 2)
  135. {
  136. builder.Append(0);
  137. }
  138. builder.Append(hv);
  139. }
  140. return builder.ToString();
  141. }
  142. }