txt文件,有不同的編碼方式,導入到系統有時不識別,故做以下適配。
#region 字節流編碼格式判斷
/// <summary>
/// 獲取txt文件內容行
/// 兼容不同的編碼方式
/// </summary>
/// <param name="stream"></param>
/// <param name="isTrim">是否去除行兩邊空白字符</param>
/// <param name="isBlank">是否包含空白行</param>
/// <returns></returns>
public static List<string> GetLinesFromTxt(Stream stream, bool isTrim = true, bool isBlank = false)
{
#region 讀取txt文件
List<string> paragraphList = new List<string>();
var encoding = SowerPower.Common.Utils.GetStreamEncoding(stream);
using (StreamReader sr = new StreamReader(stream, encoding))
{
string lineTxt = "";
while ((lineTxt = sr.ReadLine()) != null)
{
var trimTxt = lineTxt.Trim();
if (!isBlank && trimTxt.Length == 0)
{
continue;
}
if (isTrim) paragraphList.Add(trimTxt);
else paragraphList.Add(lineTxt);
}
}
//sr.Close();
//sr.Dispose();
return paragraphList;
#endregion
}
/// <summary>
/// 判斷獲取字節流 編碼格式,主要用于txt文件內容讀取
/// </summary>
/// <param name="fs"></param>
/// <returns></returns>
public static Encoding GetStreamEncoding(Stream fs)
{
fs.Position = 0;
if (fs.Length < 2) return System.Text.Encoding.Default;
System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
Byte[] buffer = br.ReadBytes(2);
fs.Position = 0;
if (buffer[0] >= 0xEF)
{
if (buffer[0] == 0xEF && buffer[1] == 0xBB)
{
return System.Text.Encoding.UTF8;
}
else if (buffer[0] == 0xFE && buffer[1] == 0xFF)
{
return System.Text.Encoding.BigEndianUnicode;
}
else if (buffer[0] == 0xFF && buffer[1] == 0xFE)
{
return System.Text.Encoding.Unicode;
}
else
{
return System.Text.Encoding.Default;
}
}
else
{
fs.Position = 0;
br = new System.IO.BinaryReader(fs);
buffer = br.ReadBytes((int)fs.Length);
fs.Position = 0;
///判斷是否不帶bom的utf8格式
if (IsUTF8BytesWithoutBOM(buffer))
{
return new UTF8Encoding(false);
}
return System.Text.Encoding.Default;
}
}
/// <summary>
/// 判斷是否不包含 bom的utf8格式
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
private static bool IsUTF8BytesWithoutBOM(byte[] data)
{
try
{
int charByteCounter = 1; //計算當前正分析的字符應還有的字節數
byte curByte; //當前分析的字節.
for (int i = 0; i < data.Length; i++)
{
curByte = data[i];
if (charByteCounter == 1)
{
if (curByte >= 0x80)
{
//判斷當前
while (((curByte <<= 1) & 0x80) != 0)
{
charByteCounter++;
}
//標記位首位若為非0 則至少以2個1開始 如:110XXXXX...........1111110X
if (charByteCounter == 1 || charByteCounter > 6)
{
return false;
}
}
}
else
{
//若是UTF-8 此時第一位必須為1
if ((curByte & 0xC0) != 0x80)
{
return false;
}
charByteCounter--;
}
}
if (charByteCounter > 1)
{
return false;
//throw new Exception("非預期的byte格式");
}
return true;
}
catch
{
return false;
}
}
#endregion