使用C#检测文本文件的编码

12
我有一组markdown文件要传递给jekyll项目,需要使用程序或API找出它们的编码格式,即UTF-8带BOM或UTF-8不带BOM或ANSI。
如果我传递了文件的位置,那么这些文件必须被列出、读取,并将编码作为结果生成。是否有相应的代码或API可用?
我已经尝试过在流阅读器中使用sr.CurrentEncoding,如Effective way to find any file's Encoding所述,但与notepad++的结果不同。
我还尝试使用https://github.com/errepi/ude(Mozilla通用字符集检测器),如https://social.msdn.microsoft.com/Forums/vstudio/en-US/862e3342-cc88-478f-bca2-e2de6f60d2fb/detect-encoding-of-the-file?forum=csharpgeneral 所建议的,在C#项目中实现ude.dll,但结果并不像notepad++那样有效,文件编码显示为UTF-8,但从程序中得到的结果为UTF-8带BOM。
但我应该从两种方式中获得相同的结果,所以问题出在哪里?

1
这并非重复问题,因为我已经尝试了其他答案来查找编码方式,但效果并不理想。 - Deepak Raj
你认为Notepad++是正确的,而其他所有解决方案都是错误的,有什么原因吗?(特别是,你为什么认为问题文件是ANSI而不是UTF-8?文件的内容是什么?)这看起来像是一个反向工程问题,旨在复制Notepad++使用的特定算法。由于它是一个闭源产品,你是否已经向他们寻求关于他们产品的信息? - Rob Napier
@RobNapier,“由于它是一个闭源产品” - 不是这样的(https://github.com/notepad-plus-plus/notepad-plus-plus)。但由于它是C ++,看起来确实是这样。 - H H
谢谢@HenkHolterman。我误读了他们的网站! - Rob Napier
对于编码错误的文件,项目将会失败。因此,在将它们传递给Jekyll项目并运行之前,我必须通过程序确保编码正确。 - Deepak Raj
显示剩余4条评论
3个回答

15
检测编码一直是一个棘手的问题,但检测BOM却非常简单。要将BOM作为字节数组获取,只需使用编码对象的GetPreamble()函数。这应该允许您通过前导来检测整个范围的编码。
现在,至于没有前导的检测UTF-8,实际上也不是很难。您可以看到,UTF8 有严格的按位规则,规定了有效序列中期望的值,并且您可以初始化一个UTF8Encoding对象以一种在这些序列不正确时会抛出异常的方式
因此,如果您首先进行BOM检查,然后进行严格的解码检查,最后回退到Win-1252编码(您所称的“ANSI”),那么您的检测就完成了。
Byte[] bytes = File.ReadAllBytes(filename);
Encoding encoding = null;
String text = null;
// Test UTF8 with BOM. This check can easily be copied and adapted
// to detect many other encodings that use BOMs.
UTF8Encoding encUtf8Bom = new UTF8Encoding(true, true);
Boolean couldBeUtf8 = true;
Byte[] preamble = encUtf8Bom.GetPreamble();
Int32 prLen = preamble.Length;
if (bytes.Length >= prLen && preamble.SequenceEqual(bytes.Take(prLen)))
{
    // UTF8 BOM found; use encUtf8Bom to decode.
    try
    {
        // Seems that despite being an encoding with preamble,
        // it doesn't actually skip said preamble when decoding...
        text = encUtf8Bom.GetString(bytes, prLen, bytes.Length - prLen);
        encoding = encUtf8Bom;
    }
    catch (ArgumentException)
    {
        // Confirmed as not UTF-8!
        couldBeUtf8 = false;
    }
}
// use boolean to skip this if it's already confirmed as incorrect UTF-8 decoding.
if (couldBeUtf8 && encoding == null)
{
    // test UTF-8 on strict encoding rules. Note that on pure ASCII this will
    // succeed as well, since valid ASCII is automatically valid UTF-8.
    UTF8Encoding encUtf8NoBom = new UTF8Encoding(false, true);
    try
    {
        text = encUtf8NoBom.GetString(bytes);
        encoding = encUtf8NoBom;
    }
    catch (ArgumentException)
    {
        // Confirmed as not UTF-8!
    }
}
// fall back to default ANSI encoding.
if (encoding == null)
{
    encoding = Encoding.GetEncoding(1252);
    text = encoding.GetString(bytes);
}

请注意,Windows-1252(美国/西欧ANSI)是一种每个字符一个字节的编码方式,这意味着其中的所有内容都产生了技术上有效的字符,因此除非您采用启发式方法},否则无法进一步检测它以将其与其他每个字符一个字节的编码方式区分开来。

1
当然,您可以将其他编码添加到这些检查中,但请小心; 一些编码具有与其他一些编码相同的BOM开头,因此您必须按正确顺序测试它们。我知道在SO上有一个问题,其中包含该列表和逻辑,但我目前找不到它。 - Nyerguds

5

死灵召唤。

  • 首先,检查字节顺序标记(Byte-Order Mark):
  • 如果不起作用,可以尝试使用Mozilla Universal Charset Detector C# port从文本内容中推断编码。
  • 如果仍无法确定编码,则返回当前区域设置、安装的用户界面语言或系统编码等信息。
  • 如果系统编码仍无法正常工作,我们可以返回ASCII或UTF8。由于UTF8的条目0-127与ASCII相同,因此我们只需返回UTF8。

示例(DetectOrGuessEncoding)

namespace SQLMerge
{


    class EncodingDetector
    {


        public static System.Text.Encoding BomInfo(string srcFile)
        {
            return BomInfo(srcFile, false);
        } // End Function BomInfo 



        public static System.Text.Encoding BomInfo(string srcFile, bool thorough)
        {
            byte[] b = new byte[5];

            using (System.IO.FileStream file = new System.IO.FileStream(srcFile, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read))
            {
                int numRead = file.Read(b, 0, 5);
                if (numRead < 5)
                    System.Array.Resize(ref b, numRead);

                file.Close();
            } // End Using file 

            if (b.Length >= 4 && b[0] == 0x00 && b[1] == 0x00 && b[2] == 0xFE && b[3] == 0xFF) // UTF32-BE 
                return System.Text.Encoding.GetEncoding("utf-32BE"); // UTF-32, big-endian 
            else if (b.Length >= 4 && b[0] == 0xFF && b[1] == 0xFE && b[2] == 0x00 && b[3] == 0x00) // UTF32-LE
                return System.Text.Encoding.UTF32; // UTF-32, little-endian
            // https://en.wikipedia.org/wiki/Byte_order_mark#cite_note-14    
            else if (b.Length >= 4 && b[0] == 0x2b && b[1] == 0x2f && b[2] == 0x76 && (b[3] == 0x38 || b[3] == 0x39 || b[3] == 0x2B || b[3] == 0x2F)) // UTF7
                return System.Text.Encoding.UTF7;  // UTF-7
            else if (b.Length >= 3 && b[0] == 0xEF && b[1] == 0xBB && b[2] == 0xBF) // UTF-8
                return System.Text.Encoding.UTF8;  // UTF-8
            else if (b.Length >= 2 && b[0] == 0xFE && b[1] == 0xFF) // UTF16-BE
                return System.Text.Encoding.BigEndianUnicode; // UTF-16, big-endian
            else if (b.Length >= 2 && b[0] == 0xFF && b[1] == 0xFE) // UTF16-LE
                return System.Text.Encoding.Unicode; // UTF-16, little-endian

            // Maybe there is a future encoding ...
            // PS: The above yields more than this - this doesn't find UTF7 ...
            if (thorough)
            {
                System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<System.Text.Encoding, byte[]>> lsPreambles = 
                    new System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<System.Text.Encoding, byte[]>>();

                foreach (System.Text.EncodingInfo ei in System.Text.Encoding.GetEncodings())
                {
                    System.Text.Encoding enc = ei.GetEncoding();

                    byte[] preamble = enc.GetPreamble();

                    if (preamble == null)
                        continue;

                    if (preamble.Length == 0)
                        continue;

                    if (preamble.Length > b.Length)
                        continue;

                    System.Collections.Generic.KeyValuePair<System.Text.Encoding, byte[]> kvp =
                        new System.Collections.Generic.KeyValuePair<System.Text.Encoding, byte[]>(enc, preamble);

                    lsPreambles.Add(kvp);
                } // Next ei

                // li.Sort((a, b) => a.CompareTo(b)); // ascending sort
                // li.Sort((a, b) => b.CompareTo(a)); // descending sort
                lsPreambles.Sort(
                    delegate (
                        System.Collections.Generic.KeyValuePair<System.Text.Encoding, byte[]> kvp1, 
                        System.Collections.Generic.KeyValuePair<System.Text.Encoding, byte[]> kvp2)
                    {
                        return kvp2.Value.Length.CompareTo(kvp1.Value.Length);
                    }
                );


                for (int j = 0; j < lsPreambles.Count; ++j)
                {
                    for (int i = 0; i < lsPreambles[j].Value.Length; ++i)
                    {
                        if (b[i] != lsPreambles[j].Value[i])
                        {
                            goto NEXT_J_AND_NOT_NEXT_I;
                        }
                    } // Next i 

                    return lsPreambles[j].Key;
                    NEXT_J_AND_NOT_NEXT_I: continue;
                } // Next j 

            } // End if (thorough)

            return null;
        } // End Function BomInfo 


        public static System.Text.Encoding DetectOrGuessEncoding(string fileName)
        {
            return DetectOrGuessEncoding(fileName, false);
        }


        public static System.Text.Encoding DetectOrGuessEncoding(string fileName, bool withOutput)
        {
            if (!System.IO.File.Exists(fileName))
                return null;


            System.ConsoleColor origBack = System.ConsoleColor.Black;
            System.ConsoleColor origFore = System.ConsoleColor.White;
            

            if (withOutput)
            {
                origBack = System.Console.BackgroundColor;
                origFore = System.Console.ForegroundColor;
            }
            
            // System.Text.Encoding systemEncoding = System.Text.Encoding.Default; // Returns hard-coded UTF8 on .NET Core ... 
            System.Text.Encoding systemEncoding = GetSystemEncoding();
            System.Text.Encoding enc = BomInfo(fileName);
            if (enc != null)
            {
                if (withOutput)
                {
                    System.Console.BackgroundColor = System.ConsoleColor.Green;
                    System.Console.ForegroundColor = System.ConsoleColor.White;
                    System.Console.WriteLine(fileName);
                    System.Console.WriteLine(enc);
                    System.Console.BackgroundColor = origBack;
                    System.Console.ForegroundColor = origFore;
                }

                return enc;
            }

            using (System.IO.Stream strm = System.IO.File.OpenRead(fileName))
            {
                UtfUnknown.DetectionResult detect = UtfUnknown.CharsetDetector.DetectFromStream(strm);

                if (detect != null && detect.Details != null && detect.Details.Count > 0 && detect.Details[0].Confidence < 1)
                {
                    if (withOutput)
                    {
                        System.Console.BackgroundColor = System.ConsoleColor.Red;
                        System.Console.ForegroundColor = System.ConsoleColor.White;
                        System.Console.WriteLine(fileName);
                        System.Console.WriteLine(detect);
                        System.Console.BackgroundColor = origBack;
                        System.Console.ForegroundColor = origFore;
                    }

                    foreach (UtfUnknown.DetectionDetail detail in detect.Details)
                    {
                        if (detail.Encoding == systemEncoding
                            || detail.Encoding == System.Text.Encoding.UTF8
                        )
                            return detail.Encoding;
                    }

                    return detect.Details[0].Encoding;
                }
                else if (detect != null && detect.Details != null && detect.Details.Count > 0)
                {
                    if (withOutput)
                    {
                        System.Console.BackgroundColor = System.ConsoleColor.Green;
                        System.Console.ForegroundColor = System.ConsoleColor.White;
                        System.Console.WriteLine(fileName);
                        System.Console.WriteLine(detect);
                        System.Console.BackgroundColor = origBack;
                        System.Console.ForegroundColor = origFore;
                    }

                    return detect.Details[0].Encoding;
                }

                enc = GetSystemEncoding();

                if (withOutput)
                {
                    System.Console.BackgroundColor = System.ConsoleColor.DarkRed;
                    System.Console.ForegroundColor = System.ConsoleColor.Yellow;
                    System.Console.WriteLine(fileName);
                    System.Console.Write("Assuming ");
                    System.Console.Write(enc.WebName);
                    System.Console.WriteLine("...");
                    System.Console.BackgroundColor = origBack;
                    System.Console.ForegroundColor = origFore;
                }

                return systemEncoding;
            } // End Using strm 

        } // End Function DetectOrGuessEncoding 


        public static System.Text.Encoding GetSystemEncoding()
        {
            // The OEM code page for use by legacy console applications
            // int oem = System.Globalization.CultureInfo.CurrentCulture.TextInfo.OEMCodePage;

            // The ANSI code page for use by legacy GUI applications
            // int ansi = System.Globalization.CultureInfo.InstalledUICulture.TextInfo.ANSICodePage; // Machine 
            int ansi = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ANSICodePage; // User 

            try
            {
                // https://dev59.com/iVkT5IYBdhLWcg3wh_1U
#if ( NETSTANDARD && !NETSTANDARD1_0 )  || NETCORE || NETCOREAPP3_0 || NETCOREAPP3_1 
                System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
#endif

                System.Text.Encoding enc = System.Text.Encoding.GetEncoding(ansi);
                return enc;
            }
            catch (System.Exception)
            { }


            try
            {

                foreach (System.Text.EncodingInfo ei in System.Text.Encoding.GetEncodings())
                {
                    System.Text.Encoding e = ei.GetEncoding();

                    // 20'127: US-ASCII 
                    if (e.WindowsCodePage == ansi && e.CodePage != 20127)
                    {
                        return e;
                    }

                }
            }
            catch (System.Exception)
            { }

            // return System.Text.Encoding.GetEncoding("iso-8859-1");
            return System.Text.Encoding.UTF8;
        } // End Function GetSystemEncoding 


    } // End Class 


}

UTF-7 这种编码方式在技术上本来就是不正确的;它是四个字节,第四个字节的最后两位属于下一个字符,因此必须使用位掩码进行检查。而且彻底的方法应该按前导长度进行预排序,最长的排在最前面,否则你会将 UTF-16 的前导与 UTF-32 匹配。 - Nyerguds
“detector”包是一个很好的参考资料。它似乎正在工作。 - James John McGuire 'Jahmic'
2
@Nyerguds:该死,UTF-7 真的有问题。已修复,并添加了预排序功能。必须非常非常仔细地阅读那些维基百科表格。 - Stefan Steiger
注意,我认为没有什么东西真正使用UTF-7。任何保存文本为UTF-7的人在无法打开时得到了他们应得的结果。 - Nyerguds
1
@Nyerguds:Quake3和Java都在使用它。现在,我们可以原谅和忘记Quake3,但是Java...例如,我多年前导入的来自瑞士邮政服务的文件,其中包括邮政编码和地名...UTF-7是我的最后猜测,但最后的猜测证明是正确的;此外,它似乎在电子邮件中也是一种东西,除了安全问题之外毫无用处。 - Stefan Steiger

-1
namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            List<FilePath> filePaths = new List<FilePath>();
            filePaths = GetLstPaths();
        }
        public static List<FilePath> GetLstPaths()
        {
            #region Getting Files

            DirectoryInfo directoryInfo = new DirectoryInfo(@"C:\Users\Safi\Desktop\ss\");
            DirectoryInfo directoryTargetInfo = new DirectoryInfo(@"C:\Users\Safi\Desktop\ss1\");
            FileInfo[] fileInfos = directoryInfo.GetFiles("*.txt");
            List<FilePath> lstFiles = new List<FilePath>();
            foreach (FileInfo fileInfo in fileInfos)
            {
                Encoding enco = GetLittleIndianFiles(directoryInfo + fileInfo.Name);
                string filePath = directoryInfo + fileInfo.Name;
                string targetFilePath = directoryTargetInfo + fileInfo.Name;
                if (enco != null)
                {
                    FilePath f1 = new FilePath();
                    f1.filePath = filePath;
                    f1.targetFilePath = targetFilePath;
                    lstFiles.Add(f1);
                }
            }
            int count = 0;
            lstFiles.ForEach(d =>
            {
                count++;
            });
            MessageBox.Show(Convert.ToString(count) + "Files are Converted");
            #endregion
            return lstFiles;
        }
        public static Encoding GetLittleIndianFiles(string srcFile)
        {
            byte[] b = new byte[5];

            using (System.IO.FileStream file = new System.IO.FileStream(srcFile, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read))
            {
                int numRead = file.Read(b, 0, 5);
                if (numRead < 5)
                    System.Array.Resize(ref b, numRead);

                file.Close();
            } // End Using file 
            if (b.Length >= 2 && b[0] == 0xFF && b[1] == 0xFE)
                return System.Text.Encoding.Unicode; // UTF-16, little-endian
            return null;
        }
    }

    public class FilePath
    {
        public string filePath { get; set; }
        public string targetFilePath { get; set; }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接