VerifyFileExtensionName.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
  2. //
  3. // 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
  4. //
  5. // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
  6. namespace Admin.NET.Core;
  7. /// <summary>
  8. /// 验证文件类型
  9. /// </summary>
  10. public static class VerifyFileExtensionName
  11. {
  12. private static readonly IDictionary<string, string> dics_ext = new Dictionary<string, string>();
  13. private static readonly IDictionary<string, HashSet<int>> ext_dics = new Dictionary<string, HashSet<int>>();
  14. static VerifyFileExtensionName()
  15. {
  16. dics_ext.Add("FFD8FFE0", ".jpg");
  17. dics_ext.Add("89504E47", ".png");
  18. dics_ext.Add("47494638", ".gif");
  19. dics_ext.Add("49492A00", ".tif");
  20. dics_ext.Add("424D", ".bmp");
  21. // PS和CAD
  22. dics_ext.Add("38425053", ".psd");
  23. dics_ext.Add("41433130", ".dwg"); // CAD
  24. dics_ext.Add("252150532D41646F6265", ".ps");
  25. // 办公文档类
  26. dics_ext.Add("D0CF11E0", ".doc"); // ppt、doc、xls
  27. dics_ext.Add("504B0304", ".docx"); // pptx、docx、xlsx
  28. /* 注意由于文本文档录入内容过多,则读取文件头时较为多变-START */
  29. dics_ext.Add("0D0A0D0A", ".txt"); // txt
  30. dics_ext.Add("0D0A2D2D", ".txt"); // txt
  31. dics_ext.Add("0D0AB4B4", ".txt"); // txt
  32. dics_ext.Add("B4B4BDA8", ".txt"); // 文件头部为汉字
  33. dics_ext.Add("73646673", ".txt"); // txt,文件头部为英文字母
  34. dics_ext.Add("32323232", ".txt"); // txt,文件头部内容为数字
  35. dics_ext.Add("0D0A09B4", ".txt"); // txt,文件头部内容为数字
  36. dics_ext.Add("3132330D", ".txt"); // txt,文件头部内容为数字
  37. /* 注意由于文本文档录入内容过多,则读取文件头时较为多变-END */
  38. dics_ext.Add("7B5C727466", ".rtf"); // 日记本
  39. dics_ext.Add("255044462D312E", ".pdf");
  40. // 视频或音频类
  41. dics_ext.Add("3026B275", ".wma");
  42. dics_ext.Add("57415645", ".wav");
  43. dics_ext.Add("41564920", ".avi");
  44. dics_ext.Add("4D546864", ".mid");
  45. dics_ext.Add("2E524D46", ".rm");
  46. dics_ext.Add("000001BA", ".mpg");
  47. dics_ext.Add("000001B3", ".mpg");
  48. dics_ext.Add("6D6F6F76", ".mov");
  49. dics_ext.Add("3026B2758E66CF11", ".asf");
  50. // 压缩包
  51. dics_ext.Add("52617221", ".rar");
  52. dics_ext.Add("504B03040A000000", ".zip");
  53. dics_ext.Add("1F8B08", ".gz");
  54. // 程序文件
  55. dics_ext.Add("3C3F786D6C", ".xml");
  56. dics_ext.Add("68746D6C3E", ".html");
  57. //dics_ext.Add("7061636B", ".java");
  58. //dics_ext.Add("3C254020", ".jsp");
  59. //dics_ext.Add("4D5A9000", ".exe");
  60. dics_ext.Add("44656C69766572792D646174653A", ".eml"); // 邮件
  61. dics_ext.Add("5374616E64617264204A", ".mdb"); // Access数据库文件
  62. dics_ext.Add("46726F6D", ".mht");
  63. dics_ext.Add("4D494D45", ".mhtml");
  64. foreach (var dics in dics_ext)
  65. {
  66. if (!ext_dics.ContainsKey(dics.Value))
  67. ext_dics.Add(dics.Value, new HashSet<int> { dics.Key.Length / 2 });
  68. else
  69. ext_dics[dics.Value].Add(dics.Key.Length / 2);
  70. }
  71. }
  72. /// <summary>
  73. /// 文件格式和文件内容格式是否一致
  74. /// </summary>
  75. /// <param name="stream"></param>
  76. /// <param name="suffix"></param>
  77. /// <returns></returns>
  78. public static bool IsSameType(Stream stream, string suffix = ".jpg")
  79. {
  80. if (stream == null)
  81. return false;
  82. suffix = suffix.ToLower();
  83. if (!ext_dics.ContainsKey(suffix))
  84. return false;
  85. try
  86. {
  87. foreach (var Len in ext_dics[suffix])
  88. {
  89. byte[] b = new byte[Len];
  90. stream.Read(b, 0, b.Length);
  91. // string fileType = System.Text.Encoding.UTF8.GetString(b);
  92. string fileKey = GetFileHeader(b);
  93. if (dics_ext.ContainsKey(fileKey))
  94. return true;
  95. }
  96. }
  97. catch (IOException)
  98. {
  99. }
  100. return false;
  101. }
  102. /**
  103. * 根据文件转换成的字节数组获取文件头信息
  104. * @param 文件路径
  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. var builder = new StringBuilder();
  125. if (src == null || src.Length <= 0)
  126. return null;
  127. string hv;
  128. for (int i = 0; i < src.Length; i++)
  129. {
  130. // 以十六进制(基数 16)无符号整数形式返回一个整数参数的字符串表示形式,并转换为大写
  131. hv = Convert.ToString(src[i] & 0xFF, 16).ToUpper();
  132. if (hv.Length < 2)
  133. builder.Append(0);
  134. builder.Append(hv);
  135. }
  136. return builder.ToString();
  137. }
  138. }