net文件压缩(net6下使用DotnetZip解压文件)
net文件压缩(net6下使用DotnetZip解压文件)Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);System.Text.Encoding.GetEncoding("gbk") 1 /// <summary> 2 /// 解压ZIP文件 3 /// </summary> 4 /// <param name="strZipPath">待解压的ZIP文件</param> 5 /// <param name="strUnZipPath">解压的目录</param> 6 /// <param name="overWrite">是否覆盖&l
在netframework环境下,使用上面文章中的设置Encoding为Default的方法即可解决中文乱码问题
但是当我使用.net6创建控制台项目并采用上述代码时,发现中文乱码问题并未得到解决。
经过整合搜索内容,发现在.netcore中若想使中文不发生乱码,需要进行如下配置
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
System.Text.Encoding.GetEncoding("gbk")
1 /// <summary>
2 /// 解压ZIP文件
3 /// </summary>
4 /// <param name="strZipPath">待解压的ZIP文件</param>
5 /// <param name="strUnZipPath">解压的目录</param>
6 /// <param name="overWrite">是否覆盖</param>
7 /// <returns>成功:true/失败:false</returns>
8 public static bool Decompression(string strZipPath string strUnZipPath bool overWrite)
9 {
10 if (string.IsNullOrEmpty(strZipPath))
11 {
12 throw new ArgumentNullException(strZipPath);
13 }
14 if (string.IsNullOrEmpty(strUnZipPath))
15 {
16 throw new ArgumentNullException(strUnZipPath);
17 }
18 try
19 {
20 var options = new ReadOptions
21 {
22 //设置编码,解决解压文件时中文乱码
23 Encoding = System.Text.Encoding.GetEncoding("gbk")
24 };
25 using (var zip = ZipFile.Read(strZipPath options))
26 {
27 foreach (var entry in zip)
28 {
29 if (string.IsNullOrEmpty(strUnZipPath))
30 {
31 strUnZipPath = strZipPath.Split('.').First();
32 }
33 entry.Extract(strUnZipPath overWrite
34 ? ExtractExistingFileAction.OverwriteSilently
35 : ExtractExistingFileAction.DoNotOverwrite);
36 }
37 return true;
38 }
39 }
40 catch (Exception ex)
41 {
42 throw new Exception(ex.Message);
43 }
44 }
注:framework程序中使用Encoding.Default即可解决中文乱码问题
但在.netcore中需要使用Encoding.GetEncoding("gbk")解决中文乱码问题