Пример #1
0
        /// <summary>
        /// 合并文件的方法
        /// </summary>
        private void StartMerge()
        {
            //清空ListView
            lvwFileInfo.Items.Clear();
            //清空进度条
            prgSchedule.Value = 0;
            //反序列化头文件对象
            FileStream      fs       = new FileStream(txtHeadFile.Text, FileMode.Open);
            BinaryFormatter bf       = new BinaryFormatter();
            HeadFile        headFile = bf.Deserialize(fs) as HeadFile;

            prgSchedule.Maximum = headFile.GetFileInfo().Length;
            //获得所有文件片段的路径信息
            Dictionary <String, String> dictionary = new Dictionary <String, String>();

            String[] urls = Directory.GetFiles(txtHeadFile.Text.Substring(0, txtHeadFile.Text.LastIndexOf('\\') + 1), "*.binary");
            foreach (Info fileInfo in headFile.GetFileInfo())
            {
                foreach (String url in urls)
                {
                    if (fileInfo.FileName.Equals(url.Substring(url.LastIndexOf('\\') + 1)))
                    {
                        //建立文件名和路径的映射
                        dictionary.Add(fileInfo.FileName, url);
                        break;
                    }
                }
            }
            //校检文件完整性
            if (CheckFile(headFile, dictionary))
            {
                AddInfo("文件校检成功,开始合并文件 - - - - - >");
                //创建文件写入流
                FileStream fileStream = new FileStream(txtFileURL.Text + "\\" + headFile.Format, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                //创建二进制写入流
                BinaryWriter binaryWriter = new BinaryWriter(fileStream);
                foreach (Info info in headFile.GetFileInfo())
                {
                    String     url          = dictionary[info.FileName];
                    FileStream fragmentFile = new FileStream(url, FileMode.Open, FileAccess.Read);
                    //创建二进制读取流
                    BinaryReader binaryReader = new BinaryReader(fragmentFile);
                    for (int rate = 0; rate < fragmentFile.Length;)
                    {
                        byte[] bytes = FillCache(binaryReader);
                        rate += bytes.Length;
                        binaryWriter.Write(bytes);
                    }
                    AddInfo("已合并文件:" + url);
                    prgSchedule.Value++;
                }
                AddInfo("文件合并完毕 - - - - - - >");
            }
            else
            {
                MessageBox.Show("文件校检失败:\n1.请检查-文件片段是否完整且都在同一文件夹下。\n2.检查-是否对文件片段有过重命名。", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Пример #2
0
        /// <summary>
        /// 文件完整性校检
        /// </summary>
        /// <param name="headFile">头文件对象</param>
        /// <param name="hashtable">文件路径映射集合</param>
        /// <returns>{true : 校检成功 , false : 校检失败}</returns>
        private bool CheckFile(HeadFile headFile, Dictionary <String, String> dictionary)
        {
            bool check = true;

            foreach (Info fileInfo in headFile.GetFileInfo())
            {
                String url = dictionary[fileInfo.FileName];
                if (fileInfo.FileSize == new FileInfo(url).Length)
                {
                    AddInfo("校检 " + url + " 成功");
                }
                else
                {
                    AddInfo("校检 " + url + " 失败,大小不匹配");
                    check = false;
                    return(check);
                }
            }
            return(check);
        }