Unity网络交互丨压缩包zip下载与解压

mac2024-01-30  31

一、注意事项

要注意PC和安卓区别:存储路径设置

 

 

二、PC版

可使用如下两种方法。

1、将Zip保存到本地,解压本地Zip

推荐第3种方法。因为本种方法在PC成功,在安卓不能解压。

string downLoadPath=写入zip的路径.zip; string unZipPath=要解压到的文件夹; //下载结束写入本地 File.WriteAllBytes(downLoadPath, 下载的数据); //解压本地zip Skode_UnzipFile(downLoadPath, unZipPath, null); public static bool Skode_UnzipFile(string _filePathName, string _outputPath, string _password = null) { try { return Skode_UnzipFile(File.OpenRead(_filePathName), _outputPath, _password); } catch (Exception _e) { Debug.LogError("[ZipUtility.UnzipFile]: " + _e.ToString()); return false; } } public static bool Skode_UnzipFile(Stream _inputStream, string _outputPath, string _password = null) { // 创建文件目录 if (!Directory.Exists(_outputPath)) Directory.CreateDirectory(_outputPath); // 解压Zip包 ZipEntry entry = null; Encoding gbk = Encoding.GetEncoding("gbk"); ZipConstants.DefaultCodePage = gbk.CodePage; using (ZipInputStream zipInputStream = new ZipInputStream(_inputStream)) { if (!string.IsNullOrEmpty(_password)) zipInputStream.Password = _password; while (null != (entry = zipInputStream.GetNextEntry())) { if (null == entry || string.IsNullOrEmpty(entry.Name)) continue; string filePathName = Path.Combine(_outputPath, entry.Name); // 创建文件目录 if (entry.IsDirectory) { Directory.CreateDirectory(filePathName); continue; } // 写入文件 try { Skode_EventHandler.instance.Skode_StartUnZip(); using (FileStream fileStream = File.Create(filePathName)) { byte[] bytes = new byte[1024]; while (true) { int count = zipInputStream.Read(bytes, 0, bytes.Length); if (count > 0) fileStream.Write(bytes, 0, count); } } } catch (System.Exception _e) { Debug.LogError("[ZipUtility.UnzipFile]: " + _e.ToString()); return false; } } } return true; }

 

2、直接将下载的byte[]解压到指定路径

直接传入下载的数据,解压出来。即省去保存为zip这一步。

注意:要引入ICSharpCode.SharpZipLib.Zip dll文件。

using ICSharpCode.SharpZipLib.Zip; using System; using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; public class SkodeUnPack: MonoBehaviour { /// <summary> /// 解压功能(下载后直接解压压缩文件到指定目录) /// </summary> /// <param name="unZipPath">解压目录</param> /// <param name="ZipByte">网络下载的数据</param> /// <param name="password">密码</param> /// <returns>解压结果</returns> public static bool SaveZip(string unZipPath, byte[] ZipByte, string password = null) { bool result = true; FileStream fs = null; ZipInputStream zipStream = null; ZipEntry ent = null; string fileName; if (!Directory.Exists(unZipPath)) { Directory.CreateDirectory(unZipPath); } try { //直接使用 将byte转换为Stream,省去先保存到本地在解压的过程 Stream stream = new MemoryStream(ZipByte); zipStream = new ZipInputStream(stream); if (!string.IsNullOrEmpty(password)) { zipStream.Password = password; } while ((ent = zipStream.GetNextEntry()) != null) { if (!string.IsNullOrEmpty(ent.Name)) { fileName = Path.Combine(unZipPath, ent.Name); #region Android fileName = fileName.Replace('\\', '/'); if (fileName.EndsWith("/")) { Directory.CreateDirectory(fileName); continue; } #endregion fs = File.Create(fileName); int size = 2048; byte[] data = new byte[size]; while (true) { size = zipStream.Read(data, 0, data.Length); if (size > 0) { //fs.Write(data, 0, data.Length); fs.Write(data, 0, size);//解决读取不完整情况 } else break; } } } } catch (Exception e) { UnityEngine.Debug.Log(e.ToString()); result = false; } finally { if (fs != null) { fs.Close(); fs.Dispose(); } if (zipStream != null) { zipStream.Close(); zipStream.Dispose(); } if (ent != null) { ent = null; } GC.Collect(); GC.Collect(1); } return result; } }

 

 

三、安卓版

安卓版使用二——2方法。但需再引用几个dll文件,否则在安卓无法解压:

在Unity搜索 I18N ,将这几个文件复制到Unity:Asset/DLL文件夹即可。

我是橙子va 认证博客专家 XR博主 前端框架 U3D开发者,AR/MR研究方向。有过联想VR项目开发、影创MR开发,也在杭电当过讲师,给网易AR上架过项目。是国内某知名电影及电视剧观众。个人联系方式:skode@foxmail.com
最新回复(0)