diff --git a/src/ZonyLrcTools.Cli/Commands/SubCommand/DownloadCommand.cs b/src/ZonyLrcTools.Cli/Commands/SubCommand/DownloadCommand.cs
index 38e8a60..096c6d9 100644
--- a/src/ZonyLrcTools.Cli/Commands/SubCommand/DownloadCommand.cs
+++ b/src/ZonyLrcTools.Cli/Commands/SubCommand/DownloadCommand.cs
@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
+using System.Text;
using System.Threading.Tasks;
using McMaster.Extensions.CommandLineUtils;
using Microsoft.Extensions.Logging;
@@ -185,9 +186,10 @@ namespace ZonyLrcTools.Cli.Commands.SubCommand
}
await using var stream = new FileStream(lyricFilePath, FileMode.Create);
- await using var sw = new StreamWriter(stream);
- await sw.WriteAsync(lyric.ToString());
- await sw.FlushAsync();
+ await using var sw = new BinaryWriter(stream);
+
+ sw.Write(EncodingConvert(lyric));
+ await stream.FlushAsync();
}
catch (ErrorCodeException ex)
{
@@ -221,6 +223,17 @@ namespace ZonyLrcTools.Cli.Commands.SubCommand
}
}
+ private byte[] EncodingConvert(LyricItemCollection lyric)
+ {
+ var supportEncodings = Encoding.GetEncodings();
+ if (supportEncodings.All(x => x.Name != _options.Provider.Lyric.Config.FileEncoding))
+ {
+ throw new ErrorCodeException(ErrorCodes.NotSupportedFileEncoding);
+ }
+
+ return Encoding.Convert(Encoding.UTF8, Encoding.GetEncoding(_options.Provider.Lyric.Config.FileEncoding), lyric.GetUtf8Bytes());
+ }
+
#endregion
#region > Ablum image download logic <
diff --git a/src/ZonyLrcTools.Cli/Config/LyricOption.cs b/src/ZonyLrcTools.Cli/Config/LyricOption.cs
index a92ac5f..6bb756f 100644
--- a/src/ZonyLrcTools.Cli/Config/LyricOption.cs
+++ b/src/ZonyLrcTools.Cli/Config/LyricOption.cs
@@ -31,4 +31,9 @@ public class LyricConfigOption
/// 如果歌词文件已经存在,是否跳过这些文件
///
public bool IsSkipExistLyricFiles { get; set; } = false;
+
+ ///
+ /// 歌词文件的编码格式。
+ ///
+ public string FileEncoding { get; set; } = "utf-8";
}
\ No newline at end of file
diff --git a/src/ZonyLrcTools.Cli/Infrastructure/Exceptions/ErrorCodes.cs b/src/ZonyLrcTools.Cli/Infrastructure/Exceptions/ErrorCodes.cs
index d9b9b18..f1be523 100644
--- a/src/ZonyLrcTools.Cli/Infrastructure/Exceptions/ErrorCodes.cs
+++ b/src/ZonyLrcTools.Cli/Infrastructure/Exceptions/ErrorCodes.cs
@@ -27,6 +27,11 @@ namespace ZonyLrcTools.Cli.Infrastructure.Exceptions
///
public const int NoFilesWereScanned = 10004;
+ ///
+ /// 文本: 指定的编码不受支持,请检查配置,所有受支持的编码名称。
+ ///
+ public const int NotSupportedFileEncoding = 10005;
+
#endregion
#region > 警告信息 <
diff --git a/src/ZonyLrcTools.Cli/Infrastructure/Lyric/LyricItemCollection.cs b/src/ZonyLrcTools.Cli/Infrastructure/Lyric/LyricItemCollection.cs
index 5300d83..badbcfe 100644
--- a/src/ZonyLrcTools.Cli/Infrastructure/Lyric/LyricItemCollection.cs
+++ b/src/ZonyLrcTools.Cli/Infrastructure/Lyric/LyricItemCollection.cs
@@ -107,5 +107,10 @@ namespace ZonyLrcTools.Cli.Infrastructure.Lyric
ForEach(lyric => lyricBuilder.Append(lyric).Append(Option.LineBreak));
return lyricBuilder.ToString().TrimEnd(Option.LineBreak);
}
+
+ public byte[] GetUtf8Bytes()
+ {
+ return Encoding.UTF8.GetBytes(ToString());
+ }
}
}
\ No newline at end of file
diff --git a/src/ZonyLrcTools.Cli/Program.cs b/src/ZonyLrcTools.Cli/Program.cs
index 55fac05..e3e73c7 100644
--- a/src/ZonyLrcTools.Cli/Program.cs
+++ b/src/ZonyLrcTools.Cli/Program.cs
@@ -1,5 +1,6 @@
using System;
using System.IO;
+using System.Text;
using System.Threading.Tasks;
using McMaster.Extensions.CommandLineUtils;
using Microsoft.Extensions.Configuration;
@@ -23,6 +24,8 @@ namespace ZonyLrcTools.Cli
{
public static async Task Main(string[] args)
{
+ Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
+
ConfigureLogger();
ConfigureErrorMessage();
@@ -94,7 +97,8 @@ namespace ZonyLrcTools.Cli
switch (ex)
{
case ErrorCodeException exception:
- Log.Logger.Error($"出现了未处理的异常。\n错误代码: {exception.ErrorCode}\n错误信息: {ErrorCodeHelper.GetMessage(exception.ErrorCode)}\n原始信息:{exception.Message}\n调用栈:{exception.StackTrace}");
+ Log.Logger.Error(
+ $"出现了未处理的异常。\n错误代码: {exception.ErrorCode}\n错误信息: {ErrorCodeHelper.GetMessage(exception.ErrorCode)}\n原始信息:{exception.Message}\n调用栈:{exception.StackTrace}");
Environment.Exit(exception.ErrorCode);
return exception.ErrorCode;
case { } unknownException:
diff --git a/src/ZonyLrcTools.Cli/Resources/error_msg.json b/src/ZonyLrcTools.Cli/Resources/error_msg.json
index 0df9598..fab9a2d 100644
--- a/src/ZonyLrcTools.Cli/Resources/error_msg.json
+++ b/src/ZonyLrcTools.Cli/Resources/error_msg.json
@@ -3,7 +3,8 @@
"10001": "待搜索的后缀不能为空。",
"10002": "需要扫描的目录不存在,请确认路径是否正确。",
"10003": "不能获取文件的后缀信息。",
- "10004": "没有扫描到任何音乐文件。"
+ "10004": "没有扫描到任何音乐文件。",
+ "10005": "指定的编码不受支持,请检查配置,所有受支持的编码名称,请参考: https://docs.microsoft.com/en-us/dotnet/api/system.text.encodinginfo.codepage?view=net-6.0#system-text-encodinginfo-codepage。"
},
"Warning": {
"50001": "扫描文件时出现了错误。",
diff --git a/src/ZonyLrcTools.Cli/ZonyLrcTools.Cli.csproj b/src/ZonyLrcTools.Cli/ZonyLrcTools.Cli.csproj
index 823bd87..910cf16 100644
--- a/src/ZonyLrcTools.Cli/ZonyLrcTools.Cli.csproj
+++ b/src/ZonyLrcTools.Cli/ZonyLrcTools.Cli.csproj
@@ -19,6 +19,7 @@
+
diff --git a/src/ZonyLrcTools.Cli/config.yaml b/src/ZonyLrcTools.Cli/config.yaml
index d703a3d..58955f1 100644
--- a/src/ZonyLrcTools.Cli/config.yaml
+++ b/src/ZonyLrcTools.Cli/config.yaml
@@ -44,4 +44,5 @@ globalOption:
isOneLine: true # 双语歌词是否合并为一行展示。
lineBreak: "\n" # 换行符的类型,记得使用双引号指定。
isEnableTranslation: true # 是否启用翻译歌词。
- isSkipExistLyricFiles: false # 如果歌词文件已经存在,是否跳过这些文件。
\ No newline at end of file
+ isSkipExistLyricFiles: false # 如果歌词文件已经存在,是否跳过这些文件。
+ fileEncoding: 'utf-8' # 歌词文件的编码格式。
\ No newline at end of file