unity中压缩文件与解压文件

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

   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后解压。

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

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值