Skip to content

Create Автоматическое определение кодировки при чтении файла.cs #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Общий код
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не помешает ссылка на форум, откуда это было взято

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

И код отформатировать не помешает, там какая-то каша местами (где фигурные скобки особенно)

namespace Analizing
{
public static class EncodingTest
{
/// <summary>
/// UTF8 : EF BB BF
/// UTF16 BE : FE FF
/// UTF16 LE : FF FE
/// UTF32 BE : 00 00 FE FF
/// UTF32 LE : FF FE 00 00
/// </summary>
public static Encoding DetectEncoding(string path)
{
FileStream fstream = new FileStream(path, FileMode.OpenOrCreate);
Encoding result=Encoding.Default;
if (!fstream.CanSeek || !fstream.CanRead){
fstream.Close();
throw new Exception("DetectEncoding() файл не может быть прочитан");
}
long Length_File = fstream.Length;
int Length_Probe_Read = 1000;
if (Length_Probe_Read >Length_File )Length_Probe_Read = Convert.ToInt32(Length_File);
Byte[] u8_Buf = new Byte[Length_Probe_Read];
int s32_Count = fstream.Read(u8_Buf, 0, Length_Probe_Read);
if (s32_Count >= 2)
{
if (u8_Buf[0] == 0xFE && u8_Buf[1] == 0xFF) {
result = new UnicodeEncoding(true, true);
}
if (u8_Buf[0] == 0xFF && u8_Buf[1] == 0xFE) {
if (s32_Count >= 4 && u8_Buf[2] == 0 && u8_Buf[3] == 0) {
result = new UTF32Encoding(false, true);
}
else {
result = new UnicodeEncoding(false, true);
}
}
if (s32_Count >= 3 && u8_Buf[0] == 0xEF && u8_Buf[1] == 0xBB && u8_Buf[2] == 0xBF) {
result = Encoding.UTF8;
}
if (s32_Count >= 4 && u8_Buf[0] == 0 && u8_Buf[1] == 0 && u8_Buf[2] == 0xFE && u8_Buf[3] == 0xFF) {
result = new UTF32Encoding(true, true);
}
// проверка по коду 0xD0
double res= 0.0; double p = 0.0 ;
for (int i=0; i<u8_Buf.Length; i++) {
if (u8_Buf[i] == 0xD0 ) res++;
}
p = res/u8_Buf.Length*100 ;
if ( p > 5 ) { // при ниличии символа 0xD0 больше чем 5%, можно предположить что кодировка UTF8
result = Encoding.UTF8;
}
/////////////////////////////
}
fstream.Close();
return result;
}
}
}

// В кубике

string path = project.Directory + @"\" + project.Variables["input"].Value ; // путь к файлу
Encoding ecn1 = Analizing.EncodingTest.DetectEncoding(path); // попытка определить кодировку
project.SendInfoToLog("Определили кодировку как : " + ecn1.ToString()); // вывод кодировки в PM

var t = File.ReadAllText(path, ecn1); // читаем весь файл
project.Variables["res"].Value = t; // ложим в переменную