DotnetZip使用方法見此文章
https://www.cnblogs.com/pengze0902/p/6124659.html
在netframework環境下,使用上面文章中的設定Encoding為Default的方法即可解決中文亂碼問題
但是當我使用.net6創建控制臺專案并采用上述代碼時,發現中文亂碼問題并未得到解決,
經過整合搜索內容,發現在.netcore中若想使中文不發生亂碼,需要進行如下配置
1.
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
2.
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")解決中文亂碼問題
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/508029.html
標籤:.NET技术
上一篇:iNeuOS工業互聯網作業系統,面向4個領域頒發第一批技術認證資質
下一篇:asp.net core 6.0網站在其他 本地服務器上都正常,部署到windowsserver2019 iis上 ,出現大于50k的圖片無法上傳
