unity中压缩文件与解压文件

今天研究了一下在unity中 把文件压缩后转二进制发送到服务器并从服务器下载后解压使用文件,废话不多说直接上代码,zip压缩插件是用的dotnetzip插件,网上可以搜索下载这个dll

csharp 复制代码
   private static void GetPathMeshData_ZIP(Milling_ProjectData data)
 {
     try
     {
         if (File.Exists(meshpath + "mesh.zip"))
         {
             File.Delete(meshpath + "mesh.zip");
         }
         ZipFile zips = new ZipFile(Encoding.Default);
         zips.AddDirectory(meshpath);//添加文件内所有文件
         zips.Save(meshpath + "mesh.zip");//保存到本地并命名
         if (File.Exists(meshpath + "mesh.zip"))
         {
             byte[] fileBytes = File.ReadAllBytes(meshpath + "mesh.zip");
             data.Binaryfile = BinaryToString(fileBytes);
             
             Debug.Log(string.Format("<color=green>{0}</color>", "字符串转换成功"));
         }

     }
     catch (Exception err)
     {
         Debug.Log(string.Format("<color=red>{0}</color>", err.Message));
     }
 }
 /// <summary>
 /// byte转字符
 /// </summary>
 /// <param name="bytes"></param>
 /// <returns></returns>
 private static string BinaryToString(byte[] bytes)
 {
     StringBuilder sb = new StringBuilder();
     foreach (byte b in bytes)
     {
         sb.Append(Convert.ToString(b, 2).PadLeft(8, '0'));
     }
     return sb.ToString();
 }
 /// <summary>
 /// 字符串转byte
 /// </summary>
 /// <param name="bytes"></param>
 /// <returns></returns>
 static byte[] StringToByteArray(string binary)
 {
     if (binary.Length % 8 != 0)
         throw new ArgumentException("Binary string length must be divisible by 8.");

     byte[] byteArray = new byte[binary.Length / 8];
     for (int i = 0; i < byteArray.Length; i++)
     {
         string eightBits = binary.Substring(i * 8, 8);
         byteArray[i] = Convert.ToByte(eightBits, 2);
     }
     return byteArray;
 }

首先判断本地文件有没有zip,如果有就删除,没有就创建并转为二进制字符串。

上传功能结束,下面是获取后台服务器二进制字符串转为byte数组在保存为本地zip后解压。

csharp 复制代码
 public static async Task LoadMillingMesh_Insobject(Milling_ProjectData data)
 {
     DirectoryInfo directory = new DirectoryInfo(meshpath);
     if (directory.Exists)
     {
         FileInfo[] files = directory.GetFiles();
         foreach (FileInfo file in files)
         {
             await Task.Run(() =>
             {
                 file.Delete();
             });
         }
     }
     var str = StringToByteArray(data.Binaryfile);
     if (File.Exists(meshpath + "mesh.zip"))
     {
         File.Delete(meshpath + "mesh.zip");
     }
     else
     {
         File.WriteAllBytes(meshpath + "mesh.zip", str);
         ReadOptions options = new ReadOptions();
         options.Encoding = Encoding.Default;//设置编码,解决解压文件时中文乱码
         ZipFile zip = ZipFile.Read(meshpath+"mesh.zip");
         for (int i = 0; i < zip.Count; i++)
         {
             zip[i].Extract(meshpath , ExtractExistingFileAction.OverwriteSilently);
         }
      
         Debug.Log(string.Format("<color=green>{0}</color>", "压缩文件下载解压成功"));
     }
     UnityEditor.AssetDatabase.Refresh();
 }

首先获取所有文件内文件 并删除,把后端传回的字符串转为byte数据,根据byte数据保存zip到本地,在使用zip.dll 解压出来进行使用。

网盘地址附上,请点赞~~~

链接:https://pan.baidu.com/s/1QzcrwiNwYZw57AFDN3cFJg?pwd=xaaw

提取码:xaaw

--来自百度网盘超级会员V6的分享

相关推荐
敲代码的 蜡笔小新37 分钟前
【行为型之访问者模式】游戏开发实战——Unity灵活数据操作与跨系统交互的架构秘诀
unity·设计模式·c#·访问者模式
Magnum Lehar9 小时前
3d游戏引擎EngineTest的系统实现3
java·开发语言·游戏引擎
动感光博10 小时前
Unity序列化字段、单例模式(Singleton Pattern)
unity·单例模式·c#
虾球xz10 小时前
游戏引擎学习第290天:完成分离渲染
c++·人工智能·学习·游戏引擎
虾球xz10 小时前
游戏引擎学习第285天:“Traversables 的事务性占用”
c++·学习·游戏引擎
虾球xz11 小时前
游戏引擎学习第280天:精简化的流式实体sim
数据库·c++·学习·游戏引擎
FAREWELL0007511 小时前
Unity基础学习(十五)核心系统——音效系统
学习·unity·c#·游戏引擎
虾球xz12 小时前
游戏引擎学习第281天:在房间之间为摄像机添加动画效果
c++·人工智能·学习·游戏引擎
向宇it13 小时前
【unity游戏开发——编辑器扩展】使用MenuItem自定义菜单栏拓展
开发语言·ui·unity·c#·编辑器·游戏引擎
动感光博13 小时前
Unity碰撞检测(射线投射、胶囊体投射)、交互(图层、掩码)
unity·c#·游戏引擎·游戏程序·动画