前段時間項目需要,搞了下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編輯器下中文路徑亂碼的問題也解決了,可以使用中文路徑