【.Net 6.0--通用帮助类--FileTypeCheckHelper】

返回首页

前言

文件格式检查帮助类,包含了以下常用的29种文件格式的检查:
1、文档格式:.DOC、.DOCX
2、PDF格式:.PDF
3、电子表格:.XLS、.XLSX
4、图片格式:.PNG、.JPG、.JPEG、.GIF、.BMP、.JFIF
5、视频格式:.MP4、.MKV、.MOV、.M4V、.WEBM、.WMV、.AVI、.FLV
6、压缩包格式:.ZIP
7、可执行文件:.EXE、.DLL、.DRV、.VXD、.SYS、.OCX、.VBX、.BAT、.DB


代码示例

using System;
using System.Collections.Generic;
using System.Linq;

namespace VW.API.Common.Utils
{
    /// 
    /// FileTypeCheckHelper 的摘要说明:文件格式检查帮助类
    /// 
    public class FileTypeCheckHelper
    {
        public enum EnumFileType
        {
            Word,
            Pdf,
            Excel,
            Image,
            Video,
            Zip,
            ExecuteFile
        };

        private readonly static Dictionary> _fileSignature = new Dictionary>
        {
            //文档格式
            { ".DOC", new List {  new byte[] { 0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1 } } },
            { ".DOCX", new List { new byte[] { 0x50, 0x4B, 0x03, 0x04 } } },
            //PDF格式
            { ".PDF", new List { new byte[] { 0x25, 0x50, 0x44, 0x46 } } },
            //电子表格
            { ".XLS", new List {
                new byte[] { 0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1 },
                new byte[] { 0x09, 0x08, 0x10, 0x00, 0x00, 0x06, 0x05, 0x00 },
                new byte[] { 0xFD, 0xFF, 0xFF, 0xFF } } },
            { ".XLSX", new List { new byte[] { 0x50, 0x4B, 0x03, 0x04 } } },
            //图片格式
            { ".PNG", new List { new byte[] { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A } } },
            { ".JPG", new List {
                new byte[] { 0xFF, 0xD8, 0xFF, 0xE0 },
                new byte[] { 0xFF, 0xD8, 0xFF, 0xE1 },
                new byte[] { 0xFF, 0xD8, 0xFF, 0xE8 } } },
            { ".JPEG", new List {
                new byte[] { 0xFF, 0xD8, 0xFF, 0xE0 },
                new byte[] { 0xFF, 0xD8, 0xFF, 0xE2 },
                new byte[] { 0xFF, 0xD8, 0xFF, 0xE3 } } },
            { ".GIF", new List { new byte[] { 0x47, 0x49, 0x46, 0x38 } } },
            { ".BMP", new List { new byte[] { 0x42, 0x4D } } },
            { ".JFIF", new List { new byte[] { 0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10, 0x4A, 0x46, 0x49, 0x46 } } },
            //视频格式
            { ".MP4", new List {
                new byte[] { 0x00,0x00,0x00,0x18,0x66,0x74,0x79,0x70,0x6D,0x70,0x34,0x32 },
                new byte[] { 0x00,0x00,0x00,0x1C,0x66,0x74,0x79,0x70,0x6D,0x70,0x34,0x32 },
                new byte[] { 0x00,0x00,0x00,0x20,0x66,0x74,0x79,0x70,0x69,0x73,0x6F, 0x6D } } },
            { ".MKV", new List { new byte[] { 0x1A, 0x45, 0xDF, 0xA3, 0xA3, 0x42, 0x86, 0x81, 0x01, 0x42, 0xF7  } } },
            { ".MOV", new List { new byte[] { 0x00, 0x00, 0x00, 0x14, 0x66, 0x74, 0x79, 0x70, 0x71, 0x74 } } },
            { ".M4V", new List { new byte[] { 0x00, 0x00, 0x00, 0x20, 0x66, 0x74, 0x79, 0x70, 0x4D, 0x34, 0x56 } } },
            { ".WEBM", new List { new byte[] { 0x1A, 0x45, 0xDF, 0xA3, 0x9F, 0x42, 0x86, 0x81, 0x01, 0x42, 0xF7, 0x81, 0x01, 0x42, 0xF2 } } },
            { ".WMV", new List { new byte[] { 0x30,0x26,0xB2,0x75,0x8E,0x66,0xCF,0x11 } } },
            { ".AVI", new List { new byte[] { 0x52, 0x49, 0x46, 0x46, 0x84, 0x4A, 0x1E, 0x00, 0x41, 0x56, 0x49 } } },
            { ".FLV", new List { new byte[] { 0x46, 0x4C, 0x56 } } },
            //压缩包
            { ".ZIP", new List {
                new byte[] { 0x50, 0x4B, 0x03, 0x04 },
                new byte[] { 0x50, 0x4B, 0x4C, 0x49, 0x54, 0x55 },
                new byte[] { 0x50, 0x4B, 0x53, 0x70, 0x58 },
                new byte[] { 0x50, 0x4B, 0x05, 0x06 },
                new byte[] { 0x50, 0x4B, 0x07, 0x08 },
                new byte[] { 0x57, 0x69, 0x6E, 0x5A, 0x69, 0x70 } } },
            //可执行文件
            { ".EXE", new List { new byte[] { 0x4D, 0x5A, 0x90, 0x00, 0x03 } } },
            { ".DLL", new List { new byte[] { 0x4D, 0x5A, 0x90, 0x00, 0x03 } } },
            { ".DRV", new List { new byte[] { 0x4D, 0x5A, 0x90, 0x00, 0x03 } } },
            { ".VXD", new List { new byte[] { 0x4D, 0x5A, 0x90, 0x00, 0x03 } } },
            { ".SYS", new List { new byte[] { 0x4D, 0x5A, 0x90, 0x00, 0x03 } } },
            { ".OCX", new List { new byte[] { 0x4D, 0x5A, 0x90, 0x00, 0x03 } } },
            { ".VBX", new List { new byte[] { 0x4D, 0x5A, 0x90, 0x00, 0x03 } } },
            { ".BAT", new List { new byte[] { 0x40, 0x65, 0x63, 0x68, 0x6F } } },
            { ".DB", new List { new byte[] { 0x53, 0x51, 0x4C, 0x69, 0x74 } } },
        };

        /// 
        /// 格式检查
        /// 
        /// 后缀
        /// 文件头字节数组,请传200个以内
        /// 
        public static bool CheckExtension(string ext, byte[] fileData)
        {
            bool flag = false;

            if (string.IsNullOrEmpty(ext) || fileData == null || fileData.Length == 0)
                return flag;

            ext = ext.ToUpperInvariant();
            if (!_fileSignature.ContainsKey(ext))
                return flag;

            List sig = _fileSignature[ext];
            foreach (byte[] b in sig)
            {
                var curFileSig = new byte[b.Length];
                Array.Copy(fileData, curFileSig, b.Length);
                if (curFileSig.SequenceEqual(b))
                {
                    //验证格式通过
                    flag = true;
                    break;
                }
            }

            return flag;
        }

        /// 
        /// 格式检查
        /// 
        /// 后缀
        /// 文件头字节数组,请传200个以内
        /// 
        public static bool CheckExtensions(string[] exts, byte[] fileData)
        {
            bool flag = false;

            foreach (string ext in exts)
            {
                flag = CheckExtension(ext, fileData);
                if (flag)
                    break;
            }

            return flag;
        }

        /// 
        /// 格式检查
        /// 
        /// 文件类型
        /// 文件头字节数组,请传200个以内
        /// 
        public static bool CheckExtensions(EnumFileType enumFileType, byte[] fileData)
        {
            bool flag = false;

            List exts = new List();
            switch (enumFileType)
            {
                case EnumFileType.Word:
                    exts.AddRange(new List { ".DOC", ".DOCX" });
                    break;
                case EnumFileType.Pdf:
                    exts.AddRange(new List { ".PDF" });
                    break;
                case EnumFileType.Excel:
                    exts.AddRange(new List { ".XLS", ".XLSX" });
                    break;
                case EnumFileType.Image:
                    exts.AddRange(new List { ".PNG", ".JPG", ".JPEG", ".GIF", ".BMP", ".JFIF" });
                    break;
                case EnumFileType.Video:
                    exts.AddRange(new List { ".MP4", ".MKV", ".MOV", ".M4V", ".WEBM", ".WMV", ".AVI", ".FLV" });
                    break;
                case EnumFileType.Zip:
                    exts.AddRange(new List { ".ZIP" });
                    break;
                case EnumFileType.ExecuteFile:
                    exts.AddRange(new List { ".EXE", ".DLL", ".DRV", ".VXD", ".SYS", ".OCX", ".VBX", ".BAT", "DB" });
                    break;
            }

            foreach (string ext in exts)
            {
                flag = CheckExtension(ext, fileData);
                if (flag)
                    break;
            }

            return flag;
        }

        /// 
        /// 格式检查
        /// 
        /// 文件类型
        /// 文件头字节数组,请传200个以内
        /// 
        public static bool CheckExtensions(EnumFileType[] enumFileTypes, byte[] fileData)
        {
            bool flag = false;

            foreach (var enumFileType in enumFileTypes)
            {
                flag = CheckExtensions(enumFileType, fileData);
                if (flag)
                    break;
            }

            return flag;
        }
    }
}

你可能感兴趣的:(.Net,通用帮助类,.net,学习)