将一个文件分割为多个文件

实现效果:

  

知识运用:

  FileStream 

  BinaryReader类   //用特定的编码将基元数据类型读作二进制值   其构造函数主要使用UTF8Encoding初始化类实例 

  public BinaryReader(Stream input)

      BinaryReader类的ReadBytes方法   //从当前流中将count个字节读入字节数组  并使当前位提升count个字节

      public virtual byte[] ReadBytes(int count)

  BinaryWriter类    //用特定的编码将基元数据类型写入流   其构造函数主要使用UTF8Encoding初始化类实例

  public BinaryWriter(Stream output)

      BinaryWriter类的Write方法        //将值写入当前流

      public virtual void Write(byte[] buffer)

实现代码:

        /// <summary>
        /// 分割文件
        /// </summary>
        /// <param name="strFlag">分割单位</param>
        /// <param name="intFlag">分割大小</param>
        /// <param name="strPath">分割后文件的存放路径</param>
        /// <param name="strFile">分割的文件</param>
        /// <param name="PBar">显示的进度条</param>
        public void SplitFile(string strFlag, int intFlag, string strPath, string strFile, ProgressBar PBar)
        {
            int iFileSize = 0;
            switch (strFlag)                                                 //根据选择来设定分割的小文件的大小
            {
                case "Byte":
                    iFileSize = intFlag;
                    break;
                case "KB":
                    iFileSize = intFlag * 1024;
                    break;
                case "MB":
                    iFileSize = intFlag * 1024 * 1024;
                    break;
                case "GB":
                    iFileSize = intFlag * 1024 * 1024 * 1024;
                    break;
            }
            FileStream fileStream = new FileStream(strFile,FileMode.Open);   //以文件全路径 和文件打开模式创建FileStream文件流对象
            BinaryReader binaryReader = new BinaryReader(fileStream);        //以FileStream文件流来创建BinaryReader文件阅读器
            byte[] tempByte;                                                 //每次分割读取的文件的最大数据
            int iFileCount = (int) (fileStream.Length / iFileSize);          //小文件总数
            if (fileStream.Length % iFileSize != 0) iFileCount++;
            PBar.Maximum = iFileCount;
            string[] tempExtra = strFile.Split('.');
            for (int i = 1; i <= iFileCount; i++)
            {
                string iTempName = strPath + @"\" + i.ToString().PadLeft(4, '0') + "." + tempExtra[tempExtra.Length- 1];
                FileStream tempStream = new FileStream(iTempName,FileMode.OpenOrCreate);
                BinaryWriter tempWriter = new BinaryWriter(tempStream);
                tempByte = binaryReader.ReadBytes(iFileSize);                //从大文件中读取指定大小的数据
                tempWriter.Write(tempByte);                                  //把此数据写入小文件
                tempWriter.Close();                                          //关闭书写器  形成小文件
                tempStream.Close();                                          //关闭流对象
                PBar.Value = i;
            }
            binaryReader.Close();                                            //关闭大文件阅读器
            fileStream.Close();                                              //关闭数据流
            MessageBox.Show("文件分割成功!");
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.SelectedIndex = 0;
        }
        //selected File
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            if(open.ShowDialog()==DialogResult.OK)
            {
                textBox1.Text = open.FileName;
            }
        }
        //save file Path
        private void button3_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fopen = new FolderBrowserDialog();
            if (fopen.ShowDialog() == DialogResult.OK)
            {
                textBox3.Text = fopen.SelectedPath;
            }
        }

猜你喜欢

转载自www.cnblogs.com/feiyucha/p/10235038.html
今日推荐