欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#判断文件编码——常用字法

程序员文章站 2022-12-06 19:43:13
使用中文写文章,当篇幅超过一定程度,必然会使用到诸如:“的”、“你”、“我”这样的常用字。本类思想便是提取中文最常用的一百个字,使用中文世界常用编码(主要有GBK、GB2312、GB18030、UTF-8、UTF-32、Unicode、BigEndianUnicode及UTF-7等)获得其编码字节, ......

  使用中文写文章,当篇幅超过一定程度,必然会使用到诸如:“的”、“你”、“我”这样的常用字。本类思想便是提取中文最常用的一百个字,使用中文世界常用编码(主要有gbkgb2312gb18030utf-8utf-32unicodebigendianunicodeutf-7等)获得其编码字节,以其为搜索词到目标流进行查找,如果查找得到则表示该流使用此种编码。显而易见此类不适用于小篇幅。

using system.collections.generic;
using system.io;
using system.text;

namespace yunshenbuzhichu.mimabenjiamifa
{
    /// <summary>
    /// 文字编码检测。
    /// 用于检测一篇文章使用什么编码方式进行编码。
    /// </summary>
    public class streambianmajiance
    {
        /// <summary>
        /// bigendianunicode编码高频汉字编码
        /// </summary>
        private list<byte[]> _bigendianunicodegaopinzifubianmalsit = new list<byte[]>()
        {
            new byte[2]{118,132},new byte[2]{78,0},new byte[2]{86,253},new byte[2]{87,40},new byte[2]{78,186},new byte[2]{78,134},new byte[2]{103,9},new byte[2]{78,45},
            new byte[2]{102,47},new byte[2]{94,116},new byte[2]{84,140},new byte[2]{89,39},new byte[2]{78,26},new byte[2]{78,13},new byte[2]{78,58},new byte[2]{83,209},
            new byte[2]{79,26},new byte[2]{93,229},new byte[2]{126,207},new byte[2]{78,10},new byte[2]{87,48},new byte[2]{94,2},new byte[2]{137,129},new byte[2]{78,42},
            new byte[2]{78,167},new byte[2]{143,217},new byte[2]{81,250},new byte[2]{136,76},new byte[2]{79,92},new byte[2]{117,31},new byte[2]{91,182},new byte[2]{78,229},
            new byte[2]{98,16},new byte[2]{82,48},new byte[2]{101,229},new byte[2]{108,17},new byte[2]{103,101},new byte[2]{98,17},new byte[2]{144,232},new byte[2]{91,249},
            new byte[2]{143,219},new byte[2]{89,26},new byte[2]{81,104},new byte[2]{94,250},new byte[2]{78,214},new byte[2]{81,108},new byte[2]{95,0},new byte[2]{78,236},
            new byte[2]{87,58},new byte[2]{92,85},new byte[2]{101,246},new byte[2]{116,6},new byte[2]{101,176},new byte[2]{101,185},new byte[2]{78,59},new byte[2]{79,1},
            new byte[2]{141,68},new byte[2]{91,158},new byte[2]{91,102},new byte[2]{98,165},new byte[2]{82,54},new byte[2]{101,63},new byte[2]{109,78},new byte[2]{117,40},
            new byte[2]{84,12},new byte[2]{78,142},new byte[2]{108,213},new byte[2]{154,216},new byte[2]{149,127},new byte[2]{115,176},new byte[2]{103,44},new byte[2]{103,8},
            new byte[2]{91,154},new byte[2]{83,22},new byte[2]{82,160},new byte[2]{82,168},new byte[2]{84,8},new byte[2]{84,193},new byte[2]{145,205},new byte[2]{81,115},
            new byte[2]{103,58},new byte[2]{82,6},new byte[2]{82,155},new byte[2]{129,234},new byte[2]{89,22},new byte[2]{128,5},new byte[2]{83,58},new byte[2]{128,253},
            new byte[2]{139,190},new byte[2]{84,14},new byte[2]{92,49},new byte[2]{123,73},new byte[2]{79,83},new byte[2]{78,11},new byte[2]{78,7},new byte[2]{81,67},
            new byte[2]{121,62},new byte[2]{143,199},new byte[2]{82,77},new byte[2]{151,98},new byte[2]{48,2},new byte[2]{255,12},new byte[2]{255,31},new byte[2]{255,1}
        };
        /// <summary>
        /// utf8编码高频汉字编码
        /// </summary>
        private list<byte[]> _utf8gaopinzifubianmalsit = new list<byte[]>()
        {
            new byte[3]{231,154,132},new byte[3]{228,184,128},new byte[3]{229,155,189},new byte[3]{229,156,168},new byte[3]{228,186,186},new byte[3]{228,186,134},
            new byte[3]{230,156,137},new byte[3]{228,184,173},new byte[3]{230,152,175},new byte[3]{229,185,180},new byte[3]{229,146,140},new byte[3]{229,164,167},
            new byte[3]{228,184,154},new byte[3]{228,184,141},new byte[3]{228,184,186},new byte[3]{229,143,145},new byte[3]{228,188,154},new byte[3]{229,183,165},
            new byte[3]{231,187,143},new byte[3]{228,184,138},new byte[3]{229,156,176},new byte[3]{229,184,130},new byte[3]{232,166,129},new byte[3]{228,184,170},
            new byte[3]{228,186,167},new byte[3]{232,191,153},new byte[3]{229,135,186},new byte[3]{232,161,140},new byte[3]{228,189,156},new byte[3]{231,148,159},
            new byte[3]{229,174,182},new byte[3]{228,187,165},new byte[3]{230,136,144},new byte[3]{229,136,176},new byte[3]{230,151,165},new byte[3]{230,176,145},
            new byte[3]{230,157,165},new byte[3]{230,136,145},new byte[3]{233,131,168},new byte[3]{229,175,185},new byte[3]{232,191,155},new byte[3]{229,164,154},
            new byte[3]{229,133,168},new byte[3]{229,187,186},new byte[3]{228,187,150},new byte[3]{229,133,172},new byte[3]{229,188,128},new byte[3]{228,187,172},
            new byte[3]{229,156,186},new byte[3]{229,177,149},new byte[3]{230,151,182},new byte[3]{231,144,134},new byte[3]{230,150,176},new byte[3]{230,150,185},
            new byte[3]{228,184,187},new byte[3]{228,188,129},new byte[3]{232,181,132},new byte[3]{229,174,158},new byte[3]{229,173,166},new byte[3]{230,138,165},
            new byte[3]{229,136,182},new byte[3]{230,148,191},new byte[3]{230,181,142},new byte[3]{231,148,168},new byte[3]{229,144,140},new byte[3]{228,186,142},
            new byte[3]{230,179,149},new byte[3]{233,171,152},new byte[3]{233,149,191},new byte[3]{231,142,176},new byte[3]{230,156,172},new byte[3]{230,156,136},
            new byte[3]{229,174,154},new byte[3]{229,140,150},new byte[3]{229,138,160},new byte[3]{229,138,168},new byte[3]{229,144,136},new byte[3]{229,147,129},
            new byte[3]{233,135,141},new byte[3]{229,133,179},new byte[3]{230,156,186},new byte[3]{229,136,134},new byte[3]{229,138,155},new byte[3]{232,135,170},
            new byte[3]{229,164,150},new byte[3]{232,128,133},new byte[3]{229,140,186},new byte[3]{232,131,189},new byte[3]{232,174,190},new byte[3]{229,144,142},
            new byte[3]{229,176,177},new byte[3]{231,173,137},new byte[3]{228,189,147},new byte[3]{228,184,139},new byte[3]{228,184,135},new byte[3]{229,133,131},
            new byte[3]{231,164,190},new byte[3]{232,191,135},new byte[3]{229,137,141},new byte[3]{233,157,162},new byte[3]{227,128,130},new byte[3]{239,188,140},
            new byte[3]{239,188,159},new byte[3]{239,188,129},
        };
        /// <summary>
        /// unicode编码高频汉字编码
        /// </summary>
        private list<byte[]> _unicodegaopinzifubianmalsit = new list<byte[]>()
        {
            new byte[2]{132,118},new byte[2]{0,78},new byte[2]{253,86},new byte[2]{40,87},new byte[2]{186,78},new byte[2]{134,78},new byte[2]{9,103},new byte[2]{45,78},
            new byte[2]{47,102},new byte[2]{116,94},new byte[2]{140,84},new byte[2]{39,89},new byte[2]{26,78},new byte[2]{13,78},new byte[2]{58,78},new byte[2]{209,83},
            new byte[2]{26,79},new byte[2]{229,93},new byte[2]{207,126},new byte[2]{10,78},new byte[2]{48,87},new byte[2]{2,94},new byte[2]{129,137},new byte[2]{42,78},
            new byte[2]{167,78},new byte[2]{217,143},new byte[2]{250,81},new byte[2]{76,136},new byte[2]{92,79},new byte[2]{31,117},new byte[2]{182,91},new byte[2]{229,78},
            new byte[2]{16,98},new byte[2]{48,82},new byte[2]{229,101},new byte[2]{17,108},new byte[2]{101,103},new byte[2]{17,98},new byte[2]{232,144},new byte[2]{249,91},
            new byte[2]{219,143},new byte[2]{26,89},new byte[2]{104,81},new byte[2]{250,94},new byte[2]{214,78},new byte[2]{108,81},new byte[2]{0,95},new byte[2]{236,78},
            new byte[2]{58,87},new byte[2]{85,92},new byte[2]{246,101},new byte[2]{6,116},new byte[2]{176,101},new byte[2]{185,101},new byte[2]{59,78},new byte[2]{1,79},
            new byte[2]{68,141},new byte[2]{158,91},new byte[2]{102,91},new byte[2]{165,98},new byte[2]{54,82},new byte[2]{63,101},new byte[2]{78,109},new byte[2]{40,117},
            new byte[2]{12,84},new byte[2]{142,78},new byte[2]{213,108},new byte[2]{216,154},new byte[2]{127,149},new byte[2]{176,115},new byte[2]{44,103},new byte[2]{8,103},
            new byte[2]{154,91},new byte[2]{22,83},new byte[2]{160,82},new byte[2]{168,82},new byte[2]{8,84},new byte[2]{193,84},new byte[2]{205,145},new byte[2]{115,81},
            new byte[2]{58,103},new byte[2]{6,82},new byte[2]{155,82},new byte[2]{234,129},new byte[2]{22,89},new byte[2]{5,128},new byte[2]{58,83},new byte[2]{253,128},
            new byte[2]{190,139},new byte[2]{14,84},new byte[2]{49,92},new byte[2]{73,123},new byte[2]{83,79},new byte[2]{11,78},new byte[2]{7,78},new byte[2]{67,81},
            new byte[2]{62,121},new byte[2]{199,143},new byte[2]{77,82},new byte[2]{98,151},new byte[2]{2,48},new byte[2]{12,255},new byte[2]{31,255},new byte[2]{1,255}
        };
        /// <summary>
        /// utf32编码高频汉字编码
        /// </summary>
        private list<byte[]> _utf32gaopinzifubianmalsit = new list<byte[]>()
        {
            new byte[4]{132,118,0,0},new byte[4]{0,78,0,0},new byte[4]{253,86,0,0},new byte[4]{40,87,0,0},new byte[4]{186,78,0,0},new byte[4]{134,78,0,0},new byte[4]{9,103,0,0},
            new byte[4]{45,78,0,0},new byte[4]{47,102,0,0},new byte[4]{116,94,0,0},new byte[4]{140,84,0,0},new byte[4]{39,89,0,0},new byte[4]{26,78,0,0},new byte[4]{13,78,0,0},
            new byte[4]{58,78,0,0},new byte[4]{209,83,0,0},new byte[4]{26,79,0,0},new byte[4]{229,93,0,0},new byte[4]{207,126,0,0},new byte[4]{10,78,0,0},new byte[4]{48,87,0,0},
            new byte[4]{2,94,0,0},new byte[4]{129,137,0,0},new byte[4]{42,78,0,0},new byte[4]{167,78,0,0},new byte[4]{217,143,0,0},new byte[4]{250,81,0,0},new byte[4]{76,136,0,0},
            new byte[4]{92,79,0,0},new byte[4]{31,117,0,0},new byte[4]{182,91,0,0},new byte[4]{229,78,0,0},new byte[4]{16,98,0,0},new byte[4]{48,82,0,0},new byte[4]{229,101,0,0},
            new byte[4]{17,108,0,0},new byte[4]{101,103,0,0},new byte[4]{17,98,0,0},new byte[4]{232,144,0,0},new byte[4]{249,91,0,0},new byte[4]{219,143,0,0},new byte[4]{26,89,0,0},
            new byte[4]{104,81,0,0},new byte[4]{250,94,0,0},new byte[4]{214,78,0,0},new byte[4]{108,81,0,0},new byte[4]{0,95,0,0},new byte[4]{236,78,0,0},new byte[4]{58,87,0,0},
            new byte[4]{85,92,0,0},new byte[4]{246,101,0,0},new byte[4]{6,116,0,0},new byte[4]{176,101,0,0},new byte[4]{185,101,0,0},new byte[4]{59,78,0,0},new byte[4]{1,79,0,0},
            new byte[4]{68,141,0,0},new byte[4]{158,91,0,0},new byte[4]{102,91,0,0},new byte[4]{165,98,0,0},new byte[4]{54,82,0,0},new byte[4]{63,101,0,0},new byte[4]{78,109,0,0},
            new byte[4]{40,117,0,0},new byte[4]{12,84,0,0},new byte[4]{142,78,0,0},new byte[4]{213,108,0,0},new byte[4]{216,154,0,0},new byte[4]{127,149,0,0},new byte[4]{176,115,0,0},
            new byte[4]{44,103,0,0},new byte[4]{8,103,0,0},new byte[4]{154,91,0,0},new byte[4]{22,83,0,0},new byte[4]{160,82,0,0},new byte[4]{168,82,0,0},new byte[4]{8,84,0,0},
            new byte[4]{193,84,0,0},new byte[4]{205,145,0,0},new byte[4]{115,81,0,0},new byte[4]{58,103,0,0},new byte[4]{6,82,0,0},new byte[4]{155,82,0,0},new byte[4]{234,129,0,0},
            new byte[4]{22,89,0,0},new byte[4]{5,128,0,0},new byte[4]{58,83,0,0},new byte[4]{253,128,0,0},new byte[4]{190,139,0,0},new byte[4]{14,84,0,0},new byte[4]{49,92,0,0},
            new byte[4]{73,123,0,0},new byte[4]{83,79,0,0},new byte[4]{11,78,0,0},new byte[4]{7,78,0,0},new byte[4]{67,81,0,0},new byte[4]{62,121,0,0},new byte[4]{199,143,0,0},
            new byte[4]{77,82,0,0},new byte[4]{98,151,0,0},new byte[4]{2,48,0,0},new byte[4]{12,255,0,0},new byte[4]{31,255,0,0},new byte[4]{1,255,0,0}
        };
        /// <summary>
        /// utf7编码高频汉字编码
        /// </summary>
        private list<byte[]> _utf7gaopinzifubianmalsit = new list<byte[]>()
        {
            new byte[5]{43,100,111,81,45},new byte[5]{43,84,103,65,45},new byte[5]{43,86,118,48,45},new byte[5]{43,86,121,103,45},new byte[5]{43,84,114,111,45},new byte[5]{43,84,111,89,45},
            new byte[5]{43,90,119,107,45},new byte[5]{43,84,105,48,45},new byte[5]{43,90,105,56,45},new byte[5]{43,88,110,81,45},new byte[5]{43,86,73,119,45},new byte[5]{43,87,83,99,45},
            new byte[5]{43,84,104,111,45},new byte[5]{43,84,103,48,45},new byte[5]{43,84,106,111,45},new byte[5]{43,85,57,69,45},new byte[5]{43,84,120,111,45},new byte[5]{43,88,101,85,45},
            new byte[5]{43,102,115,56,45},new byte[5]{43,84,103,111,45},new byte[5]{43,86,122,65,45},new byte[5]{43,88,103,73,45},new byte[5]{43,105,89,69,45},new byte[5]{43,84,105,111,45},
            new byte[5]{43,84,113,99,45},new byte[5]{43,106,57,107,45},new byte[5]{43,85,102,111,45},new byte[5]{43,105,69,119,45},new byte[5]{43,84,49,119,45},new byte[5]{43,100,82,56,45},
            new byte[5]{43,87,55,89,45},new byte[5]{43,84,117,85,45},new byte[5]{43,89,104,65,45},new byte[5]{43,85,106,65,45},new byte[5]{43,90,101,85,45},new byte[5]{43,98,66,69,45},
            new byte[5]{43,90,50,85,45},new byte[5]{43,89,104,69,45},new byte[5]{43,107,79,103,45},new byte[5]{43,87,47,107,45},new byte[5]{43,106,57,115,45},new byte[5]{43,87,82,111,45},
            new byte[5]{43,85,87,103,45},new byte[5]{43,88,118,111,45},new byte[5]{43,84,116,89,45},new byte[5]{43,85,87,119,45},new byte[5]{43,88,119,65,45},new byte[5]{43,84,117,119,45},
            new byte[5]{43,86,122,111,45},new byte[5]{43,88,70,85,45},new byte[5]{43,90,102,89,45},new byte[5]{43,100,65,89,45},new byte[5]{43,90,98,65,45},new byte[5]{43,90,98,107,45},
            new byte[5]{43,84,106,115,45},new byte[5]{43,84,119,69,45},new byte[5]{43,106,85,81,45},new byte[5]{43,87,53,52,45},new byte[5]{43,87,50,89,45},new byte[5]{43,89,113,85,45},
            new byte[5]{43,85,106,89,45},new byte[5]{43,90,84,56,45},new byte[5]{43,98,85,52,45},new byte[5]{43,100,83,103,45},new byte[5]{43,86,65,119,45},new byte[5]{43,84,111,52,45},
            new byte[5]{43,98,78,85,45},new byte[5]{43,109,116,103,45},new byte[5]{43,108,88,56,45},new byte[5]{43,99,55,65,45},new byte[5]{43,90,121,119,45},new byte[5]{43,90,119,103,45},
            new byte[5]{43,87,53,111,45},new byte[5]{43,85,120,89,45},new byte[5]{43,85,113,65,45},new byte[5]{43,85,113,103,45},new byte[5]{43,86,65,103,45},new byte[5]{43,86,77,69,45},
            new byte[5]{43,107,99,48,45},new byte[5]{43,85,88,77,45},new byte[5]{43,90,122,111,45},new byte[5]{43,85,103,89,45},new byte[5]{43,85,112,115,45},new byte[5]{43,103,101,111,45},
            new byte[5]{43,87,82,89,45},new byte[5]{43,103,65,85,45},new byte[5]{43,85,122,111,45},new byte[5]{43,103,80,48,45},new byte[5]{43,105,55,52,45},new byte[5]{43,86,65,52,45},
            new byte[5]{43,88,68,69,45},new byte[5]{43,101,48,107,45},new byte[5]{43,84,49,77,45},new byte[5]{43,84,103,115,45},new byte[5]{43,84,103,99,45},new byte[5]{43,85,85,77,45},
            new byte[5]{43,101,84,52,45},new byte[5]{43,106,56,99,45},new byte[5]{43,85,107,48,45},new byte[5]{43,108,50,73,45},new byte[5]{43,77,65,73,45},new byte[5]{43,47,119,119,45},
            new byte[5]{43,47,120,56,45},new byte[5]{43,47,119,69,45}
        };
        /// <summary>
        /// gb18030编码高频汉字编码
        /// </summary>
        private list<byte[]> _gb18030gaopinzifubianmalsit = new list<byte[]>()
        {
            new byte[2]{181,196},new byte[2]{210,187},new byte[2]{185,250},new byte[2]{212,218},new byte[2]{200,203},new byte[2]{193,203},new byte[2]{211,208},new byte[2]{214,208},new byte[2]{202,199},
            new byte[2]{196,234},new byte[2]{186,205},new byte[2]{180,243},new byte[2]{210,181},new byte[2]{178,187},new byte[2]{206,170},new byte[2]{183,162},new byte[2]{187,225},new byte[2]{185,164},
            new byte[2]{190,173},new byte[2]{201,207},new byte[2]{181,216},new byte[2]{202,208},new byte[2]{210,170},new byte[2]{184,246},new byte[2]{178,250},new byte[2]{213,226},new byte[2]{179,246},
            new byte[2]{208,208},new byte[2]{215,247},new byte[2]{201,250},new byte[2]{188,210},new byte[2]{210,212},new byte[2]{179,201},new byte[2]{181,189},new byte[2]{200,213},new byte[2]{195,241},
            new byte[2]{192,180},new byte[2]{206,210},new byte[2]{178,191},new byte[2]{182,212},new byte[2]{189,248},new byte[2]{182,224},new byte[2]{200,171},new byte[2]{189,168},new byte[2]{203,251},
            new byte[2]{185,171},new byte[2]{191,170},new byte[2]{195,199},new byte[2]{179,161},new byte[2]{213,185},new byte[2]{202,177},new byte[2]{192,237},new byte[2]{208,194},new byte[2]{183,189},
            new byte[2]{214,247},new byte[2]{198,243},new byte[2]{215,202},new byte[2]{202,181},new byte[2]{209,167},new byte[2]{177,168},new byte[2]{214,198},new byte[2]{213,254},new byte[2]{188,195},
            new byte[2]{211,195},new byte[2]{205,172},new byte[2]{211,218},new byte[2]{183,168},new byte[2]{184,223},new byte[2]{179,164},new byte[2]{207,214},new byte[2]{177,190},new byte[2]{212,194},
            new byte[2]{182,168},new byte[2]{187,175},new byte[2]{188,211},new byte[2]{182,175},new byte[2]{186,207},new byte[2]{198,183},new byte[2]{214,216},new byte[2]{185,216},new byte[2]{187,250},
            new byte[2]{183,214},new byte[2]{193,166},new byte[2]{215,212},new byte[2]{205,226},new byte[2]{213,223},new byte[2]{199,248},new byte[2]{196,220},new byte[2]{201,232},new byte[2]{186,243},
            new byte[2]{190,205},new byte[2]{181,200},new byte[2]{204,229},new byte[2]{207,194},new byte[2]{205,242},new byte[2]{212,170},new byte[2]{201,231},new byte[2]{185,253},new byte[2]{199,176},
            new byte[2]{195,230},new byte[2]{161,163},new byte[2]{163,172},new byte[2]{163,191},new byte[2]{163,161}
        };
        /// <summary>
        /// 表示需要猜测编码的流。
        /// </summary>
        private stream _thisstream;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="_canshustream">参数流</param>
        public streambianmajiance(stream _canshustream)
        {
            if (_canshustream == null || _canshustream.length < 100)
            {
                throw new system.nullreferenceexception("wenzibianmajiance构造函数参数_canshustream不能为空且长度不能小于100");
            }
            _thisstream = _canshustream;
        }
        /// <summary>
        /// 检测。
        /// </summary>
        /// <returns>流的编码</returns>
        public encoding jiance()
        {
            byte[] _streambyte = duquweizijie();
            encoding _returnencoding = encoding.utf8;
            //通过bom头来判断编码,有bom头也就没必要去猜了。
            if (genjubomcaibianma(_streambyte, out _returnencoding) == false)
            {
                _returnencoding = caijiewenzibianma(_streambyte);
            }
            return _returnencoding;
        }
        /// <summary>
        /// 读取数据为字节。
        /// </summary>
        /// <returns></returns>
        private byte[] duquweizijie()
        {
            binaryreader _binaryreader = new binaryreader(_thisstream);
            byte[] _returnbyte = new byte[_thisstream.length];
            //判断流的长度。
            if (_thisstream.length < (long)int.maxvalue)//小于整型值的情况
            {
                _binaryreader.read(_returnbyte, 0, (int)_thisstream.length);
            }
            else//大于整型值的情况
            {
                long _index = 0;
                while (_index < _returnbyte.length)
                {
                    _returnbyte.copyto(_binaryreader.readbytes(1024), _index);
                    _index = _index + 1024l;
                }
            }
            return _returnbyte;
        }
        /// <summary>
        /// 根据bom头返回编码格式。
        /// </summary>
        /// <param name="_streambyte">流的字节组。</param>
        /// <returns></returns>
        private bool genjubomcaibianma(byte[] _streambyte, out encoding _rucanencoding)
        {
            //132 49 149 51 gb-18030
            if (_streambyte[0] == 132 && _streambyte[1] == 49 && _streambyte[2] == 149 && _streambyte[3] == 51)
            {
                _rucanencoding = encoding.getencoding("gb-18030");
                return true;
            }

            //239 187 191 utf-8
            if (_streambyte[0] == 239 && _streambyte[1] == 187 && _streambyte[2] == 191)
            {
                _rucanencoding = encoding.utf8;
                return true;
            }

            //254 255 unicode
            if (_streambyte[0] == 254 && _streambyte[1] == 255)
            {
                _rucanencoding = encoding.unicode;
                return true;
            }

            if (_streambyte[0] == 255 && _streambyte[1] == 254)
            {
                //255 254 0 0 utf-32
                if (_streambyte[2] == 0 && _streambyte[3] == 0)
                {
                    _rucanencoding = encoding.utf32;
                }

                //255 254 bigendianunicode
                _rucanencoding = encoding.bigendianunicode;
                return true;
            }

            //43 47 118 utf-7
            if (_streambyte[0] == 43 && _streambyte[1] == 47 && _streambyte[2] == 118)
            {
                //[ 56 | 57 | 43 | 47 ]
                if (_streambyte[3] == 56 || _streambyte[3] == 57 || _streambyte[3] == 43 || _streambyte[3] == 47)
                {
                    _rucanencoding = encoding.utf7;
                    return true;
                }
            }

            _rucanencoding = null;
            return false;
        }
        /// <summary>
        /// 猜解文字编码,适用于没有bom头的情况。
        /// </summary>
        /// <param name="_streambyte">流的字节组。</param>
        /// <returns></returns>
        private encoding caijiewenzibianma(byte[] _streambyte)
        {
            //按照中文世界编码使用概率确定猜解顺序。         
            //utf8
            if (gaopinzijianceuft8(_streambyte) == true)
            {
                return encoding.utf8;
            }
            //gb18030
            if (gaopinzijiancegb18030(_streambyte) == true)
            {
                return encoding.getencoding("gb18030");
            }
            //utf7
            if (gaopinzijianceutf7(_streambyte) == true)
            {
                return encoding.utf7;
            }
            //utf-32任何字符都以四个字节编码,必然可以被四整除
            if (_streambyte.length % 4 == 0)
            {
                if (gaopinzijianceutf32(_streambyte) == true)//utf32
                {
                    return encoding.utf32;
                }

            }
            //bigendianunicode和unicode任何字符都以二个字节编码,必然可以被二整除
            if (_streambyte.length % 2 == 0)
            {
                //unicode
                if (gaopinzijianceunicode(_streambyte) == true)
                {
                    return encoding.unicode;
                }
                //bigendianunicod
                if (gaopinzijiancebigendianunicode(_streambyte) == true)
                {
                    return encoding.bigendianunicode;
                }
            }
            //如果上述猜解都失败了,则返回utf-8。
            return encoding.utf8;
        }
        /// <summary>
        /// 高频字检查法,uft8编码方法
        /// </summary>
        /// <param name="_streambyte">流字节组</param>
        /// <returns></returns>
        private bool gaopinzijianceuft8(byte[] _streambyte)
        {
            //命中次数。
            int _mingzhongcishu = 0;

            foreach (byte[] bshuzu in _utf8gaopinzifubianmalsit)
            {
                for (int i = 0; i < _streambyte.length; i++)
                {
                    //判断首编码是否相等
                    if (bshuzu[0] == _streambyte[i])
                    {
                        //如果首字节相等,则检查后面二个字节是否也相等
                        if ((i + 1 < _streambyte.length && _streambyte[i + 1] == bshuzu[1])
                            && (i + 2 < _streambyte.length && _streambyte[i + 2] == bshuzu[2]))
                        {
                            _mingzhongcishu++;
                        }
                    }
                }

                //utf-8有一定几率与其他编码冲突,故而增加命中次数,减少误差。
                if (_mingzhongcishu > 2)
                {
                    return true;
                }
            }

            return false;
        }
        /// <summary>
        /// 高频字检查法,unicode编码方法
        /// </summary>
        /// <param name="_streambyte">流字节组</param>
        /// <returns></returns>
        private bool gaopinzijianceunicode(byte[] _streambyte)
        {
            foreach (byte[] bshuzu in _unicodegaopinzifubianmalsit)
            {
                for (int i = 0; i < _streambyte.length; i++)
                {
                    //判断首编码是否相等
                    if (bshuzu[0] == _streambyte[i]
                        && (i + 1 < _streambyte.length && _streambyte[i + 1] == bshuzu[1]))
                    {
                        //如果首字节相等,则检查后面二个字节是否也相等
                        return true;
                    }
                }
            }
            return false;
        }
        /// <summary>
        /// 高频字检查法,utf32编码方法
        /// </summary>
        /// <param name="_streambyte">流字节组</param>
        /// <returns></returns>
        private bool gaopinzijianceutf32(byte[] _streambyte)
        {
            foreach (byte[] bshuzu in _utf32gaopinzifubianmalsit)
            {
                for (int i = 0; i < _streambyte.length; i++)
                {
                    //判断首编码是否相等
                    if (bshuzu[0] == _streambyte[i])
                    {
                        //如果首字节相等,则检查后面二个字节是否也相等
                        if ((i + 1 < _streambyte.length && _streambyte[i + 1] == bshuzu[1])
                            && (i + 2 < _streambyte.length && _streambyte[i + 2] == bshuzu[2])
                            && (i + 3 < _streambyte.length && _streambyte[i + 3] == bshuzu[3]))
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }
        /// <summary>
        /// 高频字检查法,utf7编码方法
        /// </summary>
        /// <param name="_streambyte">流字节组</param>
        /// <returns></returns>
        private bool gaopinzijianceutf7(byte[] _streambyte)
        {
            foreach (byte[] bshuzu in _utf7gaopinzifubianmalsit)
            {
                for (int i = 0; i < _streambyte.length; i++)
                {
                    //判断首编码是否相等
                    if (bshuzu[0] == _streambyte[i])
                    {
                        //如果首字节相等,则检查后面二个字节是否也相等
                        if ((i + 1 < _streambyte.length && _streambyte[i + 1] == bshuzu[1])
                            && (i + 2 < _streambyte.length && _streambyte[i + 2] == bshuzu[2])
                            && (i + 3 < _streambyte.length && _streambyte[i + 3] == bshuzu[3])
                            && (i + 4 < _streambyte.length && _streambyte[i + 4] == bshuzu[4]))
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }
        /// <summary>
        /// 高频字检查法,gb18030编码方法
        /// </summary>
        /// <param name="_streambyte">流字节组</param>
        /// <returns></returns>
        private bool gaopinzijiancegb18030(byte[] _streambyte)
        {
            foreach (byte[] bshuzu in _gb18030gaopinzifubianmalsit)
            {
                for (int i = 0; i < _streambyte.length; i++)
                {
                    //判断首编码是否相等
                    if (bshuzu[0] == _streambyte[i])
                    {
                        //如果首字节相等,则检查后面二个字节是否也相等
                        if ((i + 1 < _streambyte.length && _streambyte[i + 1] == bshuzu[0]))
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }
        /// <summary>
        /// 高频字检查法,unicode编码方法
        /// </summary>
        /// <param name="_streambyte">流字节组</param>
        /// <returns></returns>
        private bool gaopinzijiancebigendianunicode(byte[] _streambyte)
        {
            foreach (byte[] bshuzu in _bigendianunicodegaopinzifubianmalsit)
            {
                for (int i = 0; i < _streambyte.length; i++)
                {

                    //判断首编码是否相等
                    if (bshuzu[0] == _streambyte[i])
                    {
                        //如果首字节相等,则检查后面二个字节是否也相等
                        if ((i + 1 < _streambyte.length && _streambyte[i + 1] == bshuzu[1]))
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }
    }
}
//使用示例
//爬虫判断网页编码
webrequest _webrequest = webrequest.create(@"https://www.cnblogs.com/j20171203/");
webresponse _webresponse = _webrequest.getresponse();
stream _stream = _webresponse.getresponsestream();

memorystream _memorystream = new memorystream();
const int bufferlength = 1024;
byte[] buffer = new byte[bufferlength];
int actual = 1;
while (true)
{
   actual = _stream.read(buffer, 0, bufferlength);
   _memorystream.write(buffer, 0, actual);

   if (actual == 0)
   {
      break;
   }
}
_memorystream.position = 0;

streambianmajiance _streambianmajiance = new streambianmajiance(_memorystream);
encoding _jieguoencoding = _streambianmajiance.jiance();

_stream.close();
_stream.dispose();
_webresponse.close();

 

写作时间:2018-07-19

=====================================================================================

本文只代表本人的见解,可能存在错误,仅用于技术交流。如果你喜欢该文,可以扫下面的二维码打赏我(打赏敬请备注“博客园打赏”五字)。

C#判断文件编码——常用字法