文件拷贝到指定目录下面

其中使用的 Application.streamingAssetPath + “/FBXFile” 是拷贝源文件 OldFilePath 到的目标目录

       /// <summary>
        /// 拷贝文件 循环算法
        /// </summary>
        /// <param name="OldFilePath"></param>
        /// <param name="MidName"></param>
        /// <returns></returns>
        private string CopyFile(string OldFilePath , string MidName = "")
        {
            //创建目标文件夹 
            if (!Directory.Exists(Application.streamingAssetsPath + "/FBXFile"))
            {
                Directory.CreateDirectory(Application.streamingAssetsPath + "/FBXFile");
            }
            bool Next = true;
            string FileName = Path.GetFileName(OldFilePath);
            while (Next)
            {
                
                string NewFilePath = Application.streamingAssetsPath + "/FBXFile/" + MidName + FileName;
                if (File.Exists(NewFilePath))
                {
                    //如果存在同名文件
                    if (isValidFileContent(NewFilePath, OldFilePath))
                    {
                        //同名同类
                        Next = false;
                    }
                    else
                    {
                        //只同名则改名
                        if (MidName == "")
                            MidName = "1";
                        else
                        {
                            MidName = (int.Parse(MidName) + 1).ToString();
                        }
                        Next = true;
                    }
                }
                else
                {
                    File.Copy(OldFilePath, NewFilePath);
                    Next = false;
                }
            }
            return MidName + FileName;
        }

        /// <summary>
        /// 判断两个文件是否相同
        /// </summary>
        /// <param name="filePath1"></param>
        /// <param name="filePath2"></param>
        /// <returns></returns>
        public static bool isValidFileContent(string filePath1, string filePath2)
        {
            string a = Path.GetDirectoryName(filePath1);
            string b = Path.GetDirectoryName(filePath2);
            if (b == "")
                return true;
            if (a == b)
                return true;
            using (FileStream file1 = new FileStream(filePath1, FileMode.Open), file2 = new FileStream(filePath2, FileMode.Open))
            {
                return isValidFileContent(file1, file2);
            }
        }
        public static bool isValidFileContent(FileStream file1, FileStream file2)
        {
            //创建一个哈希算法对象
            HashAlgorithm hash = HashAlgorithm.Create();
            byte[] hashByte1 = hash.ComputeHash(file1);//哈希算法根据文本得到哈希码的字节数组
            byte[] hashByte2 = hash.ComputeHash(file2);
            string str1 = BitConverter.ToString(hashByte1);//将字节数组装换为字符串
            string str2 = BitConverter.ToString(hashByte2);

            return (str1 == str2);//比较哈希码


        }

        public static bool isValidFileContent(byte[] byte1, byte[] byte2)
        {
            //创建一个哈希算法对象
            HashAlgorithm hash = HashAlgorithm.Create();
            byte[] hashByte1 = hash.ComputeHash(byte1);//哈希算法根据二进制得到哈希码的字节数组
            byte[] hashByte2 = hash.ComputeHash(byte2);
            string str1 = BitConverter.ToString(hashByte1);//将字节数组装换为字符串
            string str2 = BitConverter.ToString(hashByte2);
            return (str1 == str2);//比较哈希码
        }

        /// <summary>
        /// stream 流  转 二进制
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static byte[] StreamToBytes(Stream stream)
        {
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
            // 设置当前流的位置为流的开始 
            stream.Seek(0, SeekOrigin.Begin);
            return bytes;
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值