• 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("{0}", "字符串转换成功"));
             }
    
         }
         catch (Exception err)
         {
             Debug.Log(string.Format("{0}", err.Message));
         }
     }
     /// 
     /// byte转字符
     /// 
     /// 
     /// 
     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();
     }
     /// 
     /// 字符串转byte
     /// 
     /// 
     /// 
     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;
     }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57

    首先判断本地文件有没有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("{0}", "压缩文件下载解压成功"));
         }
         UnityEditor.AssetDatabase.Refresh();
     }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    首先获取所有文件内文件 并删除,把后端传回的字符串转为byte数据,根据byte数据保存zip到本地,在使用zip.dll 解压出来进行使用。
    网盘地址附上,请点赞~~~
    链接:https://pan.baidu.com/s/1QzcrwiNwYZw57AFDN3cFJg?pwd=xaaw
    提取码:xaaw
    –来自百度网盘超级会员V6的分享

  • 相关阅读:
    B-树(B-Tree)与二叉搜索树(BST):讲讲数据库和文件系统背后的原理(读写比较大块数据的存储系统数据结构与算法原理)...
    神经网络怎么看训练效果,神经网络常用训练方法
    APP应用程序测试要点
    MySQL语句大全及用法
    Unity如何查找两个transform最近的公共parent
    类方法/静态方法
    关系型数据库存储多维指标数据
    详解:整合SSM
    python unicodedecodeerror ‘gbk‘ codec can‘t decode byte
    C++ 惯用法之 PIMPL (接口类设计技巧)
  • 原文地址:https://blog.csdn.net/qq_43687127/article/details/138190072