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

相关推荐
浅陌sss13 小时前
Unity中可靠的UDP实现
unity
虾球xz15 小时前
游戏引擎学习第107天
学习·游戏引擎
虾球xz15 小时前
游戏引擎学习第105天
前端·学习·游戏引擎
奔跑的犀牛先生18 小时前
unity学习46:反向动力学IK
unity
幻世界20 小时前
【工具插件类教学】实现运行时2D物体交互的利器Runtime2DTransformInteractor
unity·交互·运行时2d物体交互
虾球xz1 天前
游戏引擎学习第104天
学习·游戏引擎
音视频牛哥2 天前
Unity实现高性能多实例RTSP|RTMP播放器技术实践
unity·游戏引擎·音视频·实时音视频·大牛直播sdk·rtsp播放器·rtsp player
Artistation Game2 天前
三、Unity基础(主要框架)
游戏·unity·c#·游戏引擎
Edision_li2 天前
DeepSeek教unity------MessagePack-03
unity·messagepack
lalapanda2 天前
Unity学习part2
学习·unity·游戏引擎