NChardet文本文件编码探测库源码,C#,txt文件编码自动探测

时间:2016-07-28 11:56:24
【文件属性】:

文件名称:NChardet文本文件编码探测库源码,C#,txt文件编码自动探测

文件大小:112KB

文件格式:RAR

更新时间:2016-07-28 11:56:24

NChardet 源码 txt编码 文本文件编码 C#TXT

NChardet文本文件编码探测库源码,C#,txt文件编码自动探测 用于自动探测TXT文本文件编码,支持GB2312、UTF-8、ASCII等几乎所有主流编码的探测,使用方法如下: static public Encoding GetEncoding(string bookPath, ref string charsetName) { charsetName = ""; //1. Japanese //2. Chinese //3. Simplified Chinese //4. Traditional Chinese //5. Korean //6. Dont know (默认) int lang = 2;// //用指定的语参数实例化Detector Detector det = new Detector(lang); //初始化 MyCharsetDetectionObserver cdo = new MyCharsetDetectionObserver(); det.Init(cdo); //输入字符流 //Uri url = new Uri(“http://cn.yahoo.com”); //HttpWebRequest request = // HttpWebRequest)WebRequest.Create(url); //HttpWebResponse response = // (HttpWebResponse)request.GetResponse(); Stream stream = File.OpenRead(bookPath); byte[] buf = new byte[1024]; int len; bool done = false; bool isAscii = true; bool found = false; while ((len = stream.Read(buf, 0, buf.Length)) != 0) { // 探测是否为Ascii编码 if (isAscii) isAscii = det.isAscii(buf, len); // 如果不是Ascii编码,并且编码未确定,则继续探测 if (!isAscii && !done) done = det.DoIt(buf, len, false); } stream.Close(); stream.Dispose(); //调用DatEnd方法, //如果引擎认为已经探测出了正确的编码, //则会在此时调用ICharsetDetectionObserver的Notify方法 det.DataEnd(); if (isAscii) { //Console.WriteLine("CHARSET = ASCII"); found = true; } else if (cdo.Charset != null) { //Console.WriteLine("CHARSET = {0}", cdo.Charset); found = true; } if (found) { charsetName = cdo.Charset; return GetEncodingFromEncodingName(cdo.Charset); } if (!found) { charsetName = ""; string[] prob = det.getProbableCharsets(); for (int i = 0; i < prob.Length; i++) { //Console.WriteLine("Probable Charset = " + prob[i]); } return Encoding.Default; } return Encoding.Default; } static public Encoding GetEncodingFromEncodingName(string charset) { if (string.IsNullOrWhiteSpace(charset)) { charset = "gb2312"; } return Encoding.GetEncoding(charset); }


网友评论

  • 根本不能用