欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

文件拷贝到指定目录下面

程序员文章站 2022-06-16 17:20:05
...

其中使用的 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;
        }