Commit cb76ce99 authored by 罗显健's avatar 罗显健

优化更新器脚本执行工具,解决因编码不同导致的乱码报错问题。

parent 19864606
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace UpdateExecuter
{
......@@ -140,5 +141,108 @@ namespace UpdateExecuter
}
#region 文件编码相关
/// <summary>
/// 获取文件的编码格式
/// </summary>
public static Encoding GetEncoding(string fileName)
{
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
Encoding r = GetEncoding(fs);
fs.Close();
return r;
}
/// <summary>
/// 通过给定的文件流,判断文件的编码类型
/// </summary>
/// <param name="fs">文件流</param>
/// <returns>文件的编码类型</returns>
private static Encoding GetEncoding(FileStream fs)
{
//文件的字符集在Windows下有两种,一种是ANSI,一种Unicode。
//对于Unicode,Windows支持了它的三种编码方式,一种是小尾编码(Unicode),一种是大尾编码(BigEndianUnicode),一种是UTF - 8编码。
//byte[] Unicode = new byte[] { 0xFF, 0xFE };
//byte[] UnicodeBIG = new byte[] { 0xFE, 0xFF };
//byte[] UTF8 = new byte[] { 0xEF, 0xBB, 0xBF }; //BOM头
if (fs.Length < 3)
return Encoding.Default;
byte[] bytes = new byte[3];
fs.Read(bytes, 0, 3);
Encoding reVal = Encoding.GetEncoding("GB2312");
if (bytes[0] == 0xFE && bytes[1] == 0xFF)
{
reVal = Encoding.BigEndianUnicode;
}
else if (bytes[0] == 0xFF && bytes[1] == 0xFE)
{
reVal = Encoding.Unicode;
}
else
{
if (!(bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF))
{
fs.Position = 0;
}
if (IsUTF8Bytes(fs))
{
if (bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF)
reVal = new UTF8Encoding(false);
else
reVal = Encoding.UTF8;
}
}
return reVal;
}
private static byte UTF8_BYTE_MASK = 0b1100_0000;
private static byte UTF8_BYTE_VALID = 0b1000_0000;
private static bool IsUTF8Bytes(FileStream fs)
{
//BinaryReader r = new BinaryReader(fs);
byte[] bytes = new byte[1];
fs.Read(bytes, 0, 1);
//1字节 0xxxxxxx
//2字节 110xxxxx 10xxxxxx
//3字节 1110xxxx 10xxxxxx 10xxxxxx
//4字节 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
//5字节 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
//6字节 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
while (fs.Read(bytes, 0, 1) > 0)
{
if (bytes[0] < 0x80)
continue;
int cnt = 0;
byte b = bytes[0];
while ((b & 0b1000_0000) != 0)
{
cnt++;
b <<= 1;
}
cnt -= 1;
for (int i = 0; i < cnt; i++)
{
if (fs.Read(bytes, 0, 1) <= 0)
return false;
if ((bytes[0] & UTF8_BYTE_MASK) != UTF8_BYTE_VALID)
return false;
}
}
return true;
}
#endregion
}
}
......@@ -117,7 +117,7 @@ namespace UpdateExecuter
{
if (lstClient.Items[i].Text == newClient && lstClient.Items[i] != currentItem)
{
MessageBox.Show($"客户[{ newClient}]已存在,请确认!");
MessageBox.Show($"客户[{newClient}]已存在,请确认!");
return false;
}
}
......@@ -166,7 +166,7 @@ namespace UpdateExecuter
{
btnExecute.Enabled = false;
Debug.WriteLine($"当前线程:{System.Threading.Thread.CurrentThread.ManagedThreadId }");
Debug.WriteLine($"当前线程:{System.Threading.Thread.CurrentThread.ManagedThreadId}");
if (!CheckExecuter()) return;
......@@ -185,7 +185,7 @@ namespace UpdateExecuter
// 根据文件名排序、保证按顺序执行(脚本文件有依赖顺序)
Array.Sort(dbFiles, delegate (FileInfo x, FileInfo y) { return x.FullName.CompareTo(y.FullName); });
Debug.WriteLine($"当前线程1:{System.Threading.Thread.CurrentThread.ManagedThreadId }");
Debug.WriteLine($"当前线程1:{System.Threading.Thread.CurrentThread.ManagedThreadId}");
await ExecDBScriptAsync(lstClientName, dbFiles);
}
......@@ -335,10 +335,10 @@ namespace UpdateExecuter
listViewItem.BackColor = System.Drawing.Color.Yellow;
}
Debug.WriteLine($"当前线程4:{System.Threading.Thread.CurrentThread.ManagedThreadId }");
Debug.WriteLine($"当前线程4:{System.Threading.Thread.CurrentThread.ManagedThreadId}");
}));
Debug.WriteLine($"当前线程5:{System.Threading.Thread.CurrentThread.ManagedThreadId }");
Debug.WriteLine($"当前线程5:{System.Threading.Thread.CurrentThread.ManagedThreadId}");
}
}
});
......@@ -381,7 +381,9 @@ namespace UpdateExecuter
List<string> lstSql = null;
if (!conDicFileScript.ContainsKey(file.FullName))
{
lstSql = Utils.SliceSingleFileScript(File.ReadAllText(file.FullName, System.Text.Encoding.Default));
var encoding = Utils.GetEncoding(file.FullName);
var strContent = File.ReadAllText(file.FullName, encoding);
lstSql = Utils.SliceSingleFileScript(strContent);
conDicFileScript.TryAdd(file.FullName, lstSql);
}
else
......@@ -432,11 +434,11 @@ namespace UpdateExecuter
{
string clientName = client.ClientName;
Debug.WriteLine($"当前线程2:{System.Threading.Thread.CurrentThread.ManagedThreadId }");
Debug.WriteLine($"当前线程2:{System.Threading.Thread.CurrentThread.ManagedThreadId}");
await Task.Run<bool>(() =>
{
Debug.WriteLine($"当前线程3:{System.Threading.Thread.CurrentThread.ManagedThreadId }");
Debug.WriteLine($"当前线程3:{System.Threading.Thread.CurrentThread.ManagedThreadId}");
var result = UploadUpdateFile(localUpdatePath, client);
......@@ -453,13 +455,13 @@ namespace UpdateExecuter
listViewItem.BackColor = System.Drawing.Color.Yellow;
}
Debug.WriteLine($"当前线程4:{System.Threading.Thread.CurrentThread.ManagedThreadId }");
Debug.WriteLine($"当前线程4:{System.Threading.Thread.CurrentThread.ManagedThreadId}");
}));
return result;
});
Debug.WriteLine($"当前线程5:{System.Threading.Thread.CurrentThread.ManagedThreadId }");
Debug.WriteLine($"当前线程5:{System.Threading.Thread.CurrentThread.ManagedThreadId}");
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment