修改指定文件夹中文本文件为UTF8(无BOOM)格式

using UnityEditor;
using System.IO;
using System.Text;

public class ReplaceEncodingToUTF8False : Editor
{
    static string luaPath = "Assets/Lua";

    [MenuItem("Tools/Encode/替换Lua文件编码格式为UTF-8(无Boom)")]
    public static void ReplaceEnocidngButton()
    {
        Replace();
    }

    /// <summary>
    /// 替换
    /// </summary>
    static void Replace()
    {
        DirectoryInfo dir = new DirectoryInfo(luaPath);
        FileInfo[] luaFiles = dir.GetFiles("*.lua", SearchOption.AllDirectories);
        UTF8Encoding utf8 = new UTF8Encoding(false);

        foreach (var file in luaFiles)
        {
            string fullName = file.FullName;
            string contents = File.ReadAllText(fullName, GetEncoding(fullName));
            File.WriteAllText(fullName, contents, utf8);
        }
    }

    /// <summary>
    /// 获取文件的编码格式
    /// </summary>
    /// <returns>encoding</returns>
    static Encoding GetEncoding(string filePath)
    {
        FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
        Encoding encoding = GetEncoding(fileStream);
        fileStream.Close();
        return encoding;
    }

    /// <summary>
    /// 获取文件的编码格式
    /// </summary>
    /// <returns>encoding</returns>
    static Encoding GetEncoding(FileStream fs)
    {
        byte[] Unicode = new byte[] { 0xFF, 0xFE, 0x41 };
        byte[] UnicodeBIG = new byte[] { 0xFE, 0xFF, 0x00 };
        byte[] UTF8 = new byte[] { 0xEF, 0xBB, 0xBF };  //带Bom
        Encoding encoding = Encoding.Default;
        BinaryReader br = new BinaryReader(fs, Encoding.Default);
        int i;
        int.TryParse(fs.Length.ToString(), out i);
        byte[] bytes = br.ReadBytes(i);

        if (IsUTF8Bytes(bytes) || bytes[0] == UTF8[0] && bytes[1] == UTF8[1] && bytes[2] == UTF8[2])
        {
            encoding = Encoding.UTF8;
        }
        else if (bytes[0] == Unicode[0] && bytes[1] == Unicode[1] && bytes[2] == Unicode[2])
        {
            encoding = Encoding.Unicode;
        }
        else if (bytes[0] == Unicode[0] && bytes[1] == Unicode[1] && bytes[2] == Unicode[2])
        {
            encoding = Encoding.BigEndianUnicode;
        }
        br.Close();
        return encoding;
    }

    /// <summary>
    /// 判断是否是不带BOM的UTF8格式
    /// </summary>
    /// <param name="bytes"></param>
    /// <returns></returns>
    static bool IsUTF8Bytes(byte[] bytes)
    {
        int charByteCounter = 1;  //计算当前正在分析的字符应还有的字节数
        byte curByte;  //当前分析的字节
        for (int i = 0; i < bytes.Length; i++)
        {
            curByte = bytes[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)
        {
            throw new System.Exception("非语气的byte格式");
        }
        return true;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cchoop

有用的话请杯肥宅水

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值