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

相关推荐
三只坚果11 小时前
blender制作动画导入unity两种方式
unity·游戏引擎·blender
benben04412 小时前
《Unity Shader入门精要》学习笔记二
unity·unity shader
YF云飞13 小时前
Unity音频管理:打造沉浸式游戏音效
游戏·unity·游戏引擎·游戏程序·个人开发
SmalBox19 小时前
【渲染流水线】[逐片元阶段]-[裁剪测试]以UnityURP为例
unity·渲染
与火星的孩子对话1 天前
Unity高级开发:反射原理深入解析与实践指南 C#
java·unity·c#·游戏引擎·lucene·反射
scoone1 天前
开源游戏引擎Bevy 和 Godot
游戏引擎·godot
阿赵3D1 天前
Unity2022打包安卓报错的奇葩问题
android·unity·安卓
霸王•吕布1 天前
游戏引擎中的粒子系统
游戏引擎·粒子系统·粒子发射盒·粒子物理参数·粒子实例·粒子生命周期·粒子参数
小徐小徐编程不急1 天前
unity实现背包拖拽排序
unity·游戏引擎
萘柰奈2 天前
Unity进阶--C#补充知识点--【Unity跨平台的原理】Mono与IL2CPP
unity·c#·游戏引擎