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的分享

相关推荐
仙魁XAN4 小时前
Unity 设计模式 之 创造型模式-【工厂方法模式】【抽象工厂模式】
unity·设计模式·工厂方法模式·抽象工厂模式
我要吐泡泡了哦6 小时前
GAMES104:15 游戏引擎的玩法系统基础-学习笔记
笔记·学习·游戏引擎
躺下睡觉~15 小时前
Unity-Transform类-父子关系
java·unity·游戏引擎
躺下睡觉~15 小时前
Unity-Transform类-缩放和看向
unity·游戏引擎
君莫愁。17 小时前
【Unity】检测鼠标点击位置是否有2D对象
unity·c#·游戏引擎
咩咩觉主18 小时前
Unity实战案例全解析:PVZ 植物卡片状态分析
unity·c#·游戏引擎
蓝裕安21 小时前
伪工厂模式制造敌人
开发语言·unity·游戏引擎
谢泽浩1 天前
Unity 给模型贴上照片
unity·游戏引擎
z2014z1 天前
Unity Resource System 优化笔记
unity·游戏引擎
王维志1 天前
Unity 高亮插件HighlightPlus介绍
unity·游戏引擎