1. 程式人生 > >unity解壓縮zip釋出後的一些問題

unity解壓縮zip釋出後的一些問題

  前段時間專案需要,搞了下zip的解壓縮問題,也是利用ICSharpCode.SharpZipLib.dll來處理的zip,這裡說下之前遇到的坑(這裡提供我用的這個庫ICSharpCode.SharpZipLib.dll    ;http://note.youdao.com/noteshare?id=ce22c848c004c3be99c67ecb24f991fd&sub=E60263C2B3B54CEEBA584A23AACC8069)

  一個簡單呼叫:

    /// <summary>
    /// 壓縮Zip
    /// </summary>
    ///
<param name="fileNames"></param> /// <param name="outputFilePath"></param> /// <param name="compressLevel">壓縮等級</param> public static void ZipFile(string[] fileNames, string outPath, int compressLevel) { try { using (ZipOutputStream stream = new
ZipOutputStream(File.Create(outPath))) { stream.SetLevel(compressLevel); byte[] buffer = new byte[4096]; foreach (string file in fileNames) { var entry = new ZipEntry(Path.GetFileName(file)) { DateTime
= DateTime.Now }; stream.PutNextEntry(entry); using (FileStream fs = File.OpenRead(file)) { int sourceBytes; do { sourceBytes = fs.Read(buffer, 0, buffer.Length); stream.Write(buffer, 0, sourceBytes); } while (sourceBytes > 0); } } stream.Finish(); stream.Close(); Debug.Log("完成壓縮"); } } catch (Exception e) { Debug.Log ("壓縮出錯:" + e); } } /// <summary> /// 解壓 /// </summary> /// <param name="zipPath">壓縮檔案路徑</param> /// <param name="outPath">解壓出去路徑</param> public static void UnZipFile(string zipPath, string outPath) { if (File.Exists(zipPath)) { using (ZipInputStream stream = new ZipInputStream(File.OpenRead(zipPath))) { ZipEntry theEntry; while ((theEntry = stream.GetNextEntry()) != null) { string fileName = Path.GetFileName(theEntry.Name); string filePath = Path.Combine(outPath, theEntry.Name); string directoryName = Path.GetDirectoryName(filePath); if (directoryName.Length > 0) Directory.CreateDirectory(directoryName); if (fileName != String.Empty) { using (FileStream streamWriter = File.Create(filePath)) { int size = 2048; byte[] data = new byte[2048]; while (true) { size = stream.Read(data, 0, data.Length); if (size > 0) streamWriter.Write(data, 0, size); else break; } } } } Debug.Log("解壓完成"); } } else { Debug.LogError("沒找到該檔案 : " + zipPath); } }

  也可以參考這位大佬的 https://www.jianshu.com/p/acc3d79d93f7

 

這種在untiy編輯器下處理的檔案,對路徑很敏感,不允許有任何中文,包括解壓zip時包裡面的壓縮檔名也不允許有中文,不然就會出現亂碼,雖然不能用中文,但也無關緊要了,畢竟路徑不用中文就好了嘛  O(∩_∩)O哈哈~

然而,這還不是重點,因為我們的專案始終都是要釋出的,不可永遠停留在編輯器下,釋出出來之後,執行我們的程式無論如何執行這解壓縮的方法都是沒反映,程式也沒崩,開啟我們的日誌看一下,發現有條報錯 ystem.NotSupportedException: CodePage 437 not supported 程式碼包不支援

後來查了很多資料測試很久才解決......

1.開啟unity PlayerSettings,把裡面的Scripting Runtime Version 改為 .net4.6,然後重新發布

2.釋出完成後,在unity的安裝目錄下\Editor\Data\Mono\lib\mono\unity,找到   I18N.dll 和 I18N.CJK.dll  兩個檔案,把他們倆拷貝到釋出包   **_Data/Managed目錄下。(之前因為釋出後讀取中文亂碼的問題,也是把這兩兄弟copy進取就解決的)

 

這兩步完成就可以了,而且在unity編輯器下中文路徑亂碼的問題也解決了,可以使用中文路徑