Example #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);
            }
        }
Example #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);
        }
Example #3
0
        /// <summary>
        /// 分割文件的方法
        /// </summary>
        /// <param name="mainFile">被分割文件的路径</param>
        /// <param name="outURL">分割后文件的路径</param>
        /// <param name="count">分割数量</param>
        private void StartDivision()
        {
            //清空ListView
            lvwFileInfo.Items.Clear();
            //创建指向用户选择文件的读取流
            fileStream = new FileStream(fileURL, FileMode.Open);
            //计算分割文件的数量并输出在ListView中
            AddInfo("正在计算分割后文件的数量……");
            int count = ComputingFileSize(cmbUnit.Text, fileStream.Length, (int)nudFileSize.Value);

            AddInfo("分割后文件的数量为:" + count);
            String outURL   = txtFileOutURL.Text;
            String fileName = txtFile.Text;

            //创建头文件对象
            headFile        = new HeadFile(count);
            headFile.Format = txtFile.Text;
            //创建指向用户选择文件的读取流
            binaryReader = new BinaryReader(fileStream);
            for (int i = 1; i <= count; i++)
            {
                if (isStop)//安全退出该方法和线程
                {
                    AddInfo("已终止操作  - - - - - - - - >");
                    fileStream.Close();
                    binaryReader.Close();
                    return;
                }
                long fileSize = i == count ? lastFileLenght : fileLenght;
                //分割后文件的全称
                String fileURL = outURL + "\\" + fileName.Substring(0, fileName.LastIndexOf('.')) + i + ".binary";
                //加入信息到ListView中
                AddInfo("创建文件:" + fileURL);
                //二进制数据写入对象
                using (BinaryWriter binaryWriter = new BinaryWriter(new FileStream(fileURL, FileMode.OpenOrCreate, FileAccess.ReadWrite)))
                {
                    for (int rate = 0; !(rate == fileSize);)
                    {
                        byte[] bytes = FillCache(binaryReader);
                        //进行迭代计算
                        rate += bytes.Length;
                        //将缓存区的数据写入文件
                        binaryWriter.Write(bytes);
                        //添加信息进ListView中
                        //AddInfo("以写入数据:" + rate + " BB");
                    }
                }
                //添加信息到头文件中
                headFile.AddFileInfo(new Info(fileName.Substring(0, fileName.LastIndexOf('.')) + i + ".binary", fileSize));
                //进度条前进
                prgSchedule.Value++;
            }
            //序列化头文件对象
            String          headFileURL = outURL + "\\" + fileName.Substring(0, fileName.LastIndexOf('.')) + ".head";
            Stream          fStream     = new FileStream(headFileURL, FileMode.Create, FileAccess.ReadWrite);
            BinaryFormatter binFormat   = new BinaryFormatter();//创建二进制序列化器

            binFormat.Serialize(fStream, headFile);
            AddInfo("创建头文件" + headFileURL);
            AddInfo("本次文件分解完毕……");
            //释放资源
            btnStop.Enabled = false;
            fStream.Close();
            fileStream.Close();
            binaryReader.Close();
        }