必威体育Betway必威体育官网
当前位置:首页 > IT技术

文件分割器

时间:2019-07-18 18:42:13来源:IT技术作者:seo实验室小编阅读:53次「手机版」
 

文件分割器

程序预览:

窗体代码

  1. using System;
  2. using System.Collections.Generic;
  3. using System.componentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. //using System.Linq;
  7. using System.Text;
  8. using System.windows.Forms;
  9. using System.IO;
  10. using System.Threading;
  11. namespace 文件分割器
  12. {
  13.     public partial class fmMain : Form
  14.     {
  15.         private string SplitFileName ="";
  16.         private string CombineFileName = "";
  17.         public fmMain()
  18.         {
  19.             InitializeComponent();
  20.         }
  21.         /// <summary>
  22.         /// 选择要分割的文件
  23.         /// </summary>
  24.         /// <param name="sender"></param>
  25.         /// <param name="e"></param>
  26.         private void btnSSelect_Click(object sender, EventArgs e)
  27.         {
  28.             try
  29.             {
  30.                 OpenFileDialog ofd = new OpenFileDialog();
  31.                 ofd.Filter = "所有文件(*.*)|*.*";
  32.                 pgbSplit.Value = 0;
  33.                 if (ofd.ShowDialog().Equals(dialogresult.OK))
  34.                 {
  35.                     SplitFileName = ofd.FileName;
  36.                     FileInfo fi = new FileInfo(SplitFileName);
  37.                     lblSFileInfo.Text = "文件信息:/n" +
  38.                                         "文件名:" + fi.Name + "/n" +
  39.                                         "路径:" + fi.DirectoryName + "/n" +
  40.                                         "大小:" + fi.Length.ToString("#,###") + "Bytes(" + (fi.Length/1024/1024).ToString("#,###") + "MB)/n" +
  41.                                         "属性:" + (fi.IsReadOnly ? "只读/n" : "/n") +
  42.                                         "创建时间:" + fi.CreationTime.ToLongDateString() + " " + fi.CreationTime.ToLongTimeString();
  43.                     btnSplit.Enabled = true;                   
  44.                 }
  45.                 ofd.Dispose();
  46.             }
  47.             catch (Exception ex)
  48.             {
  49.                 messageBox.Show(ex.Message);
  50.             }
  51.         }
  52.         /// <summary>
  53.         /// 初始化
  54.         /// </summary>
  55.         /// <param name="sender"></param>
  56.         /// <param name="e"></param>
  57.         private void fmMain_Load(object sender, EventArgs e)
  58.         {
  59.             try
  60.             {
  61.                 btnSplit.Enabled = false;
  62.                 lblAbout.Links.Add(113, 24, "mailto:Vicoman(MSN Email)<[email protected]>");
  63.                 lblAbout.Links.Add(146, 15, "mailto:Vicoman<[email protected]>");
  64.                 lblAbout.Links.Add(174, 28, "http://blog.csdn.net/vicoman");
  65.             }
  66.             catch(Exception ex)
  67.             {
  68.                 MessageBox.Show(ex.Message);
  69.             }
  70.         }
  71.         /// <summary>
  72.         /// 分割选定文件
  73.         /// </summary>
  74.         /// <param name="sender"></param>
  75.         /// <param name="e"></param>
  76.         private void btnSplit_Click(object sender, EventArgs e)
  77.         {
  78.             try
  79.             {
  80.                 if (txtSBlockName.Text.Trim().Equals(string.empty))
  81.                 {
  82.                     MessageBox.Show(this,"请输入分块文件的扩展名。", "【分块扩展名】不能为空", MessageBoxButtons.OK, MessageBoxIcon.ERROR);
  83.                     txtSBlockName.Focus();
  84.                     return;
  85.                 }
  86.                 if (cmbSize.Text.Trim().Equals(string.Empty))
  87.                 {
  88.                     MessageBox.Show(this, "请输入分块文件的大小。", "【分块大小】不能为空", MessageBoxButtons.OK, MessageBoxIcon.Error);
  89.                     cmbSize.Focus();
  90.                     return;
  91.                 }
  92.                 pnlST.Enabled = false;
  93.                 this.Cursor = Cursors.WaitCursor;
  94.                 if (File.exists(SplitFileName))
  95.                 {
  96.                     FileSpliter fs = new FileSpliter(SplitFileName);
  97.                     fs.ExtName = txtSBlockName.Text.Trim();
  98.                     fs.BlockSize = int.Parse((cmbSize.Text.Replace(",", "")));
  99.                     string sInfo = lblSFileInfo.Text;
  100.                     ThreadStart myThreadDelegate = new ThreadStart(fs.Split);
  101.                     Thread newThread = new Thread(myThreadDelegate);
  102.                     newThread.Start();
  103.                     while (!newThread.IsAlive);
  104.                     while (newThread.IsAlive)
  105.                     {
  106.                         APPlication.DoEvents();
  107.                         pgbSplit.Value=fs.progress;
  108.                         lblSFileInfo.Text = sInfo + "/n正在分割,已完成" + pgbSplit.Value + "%";
  109.                     }
  110.                     newthread.join();
  111.                     lblSFileInfo.Text = sInfo + "/n分割完成!分块数:" + fs.BlockCount;
  112.                 }
  113.             }
  114.             catch (Exception ex)
  115.             {
  116.                 MessageBox.Show(ex.Message);
  117.             }
  118.             finally
  119.             {
  120.                 pnlST.Enabled = true;
  121.                 this.Cursor = Cursors.Default;
  122.             }
  123.         }
  124.         /// <summary>
  125.         /// 选择合并文件的第一分块,并检查所有分块的完整性
  126.         /// </summary>
  127.         /// <param name="sender"></param>
  128.         /// <param name="e"></param>
  129.         private void btnCSelect_Click(object sender, EventArgs e)
  130.         {
  131.             try
  132.             {
  133.                 OpenFileDialog ofd = new OpenFileDialog();
  134.                 ofd.Filter = "第一个分块文件(*." + txtCBlockName.Text + "1)|*." + txtCBlockName.Text + "1";
  135.                 if (ofd.ShowDialog().Equals(DialogResult.OK))
  136.                 {
  137.                     CombineFileName = ofd.FileName;
  138.                     lblCFileInfo.Text = "正在检查分块文件完整性......";
  139.                     FileSpliter fs = new FileSpliter(CombineFileName);
  140.                     ThreadStart myThreadDelegate = new ThreadStart(fs.CheckBlockEntire);
  141.                     Thread newThread = new Thread(myThreadDelegate);
  142.                     newThread.Start();
  143.                     while (!newThread.IsAlive) ;
  144.                     while (newThread.IsAlive)
  145.                     {
  146.                         Application.DoEvents();
  147.                         pgbCombine.Value = fs.Progress;
  148.                     }
  149.                     newThread.Join();
  150.                     FileInfo fi = new FileInfo(CombineFileName);
  151.                     string sInfo = "文件信息:/n" +
  152.                                 "文件名:" + fi.Name + "/n" +
  153.                                 "路径:" + fi.DirectoryName + "/n" +
  154.                                 "大小:" + fi.Length.ToString("#,###") + "Bytes(" +
  155.                                 (fi.Length / 1024 / 1024).ToString("#,###") + "MB)/n" +
  156.                                 "创建时间:" + fi.CreationTime.ToLongDateString() + " " +
  157.                                 fi.CreationTime.ToLongTimeString();
  158.                     if (fs.ErrMsg.Length > 0)
  159.                     {
  160.                         lblCFileInfo.Text = sInfo + fs.ErrMsg;
  161.                         btnCombine.Enabled = false;
  162.                     }
  163.                     else
  164.                     {
  165.                         lblCFileInfo.Text = sInfo + "/n分块文件完整性检查成功,分块数:" + 
  166.                                             fs.BlockCount + ",请点击/"开始合并/"进行合并。";
  167.                         btnCombine.Enabled = true;
  168.                     }
  169.                     pgbCombine.Value = 0;
  170.                 }
  171.                 ofd.Dispose();
  172.             }
  173.             catch (Exception ex)
  174.             {
  175.                 MessageBox.Show(ex.Message);
  176.             }
  177.         }
  178.         /// <summary>
  179.         /// 合并所选文件
  180.         /// </summary>
  181.         /// <param name="sender"></param>
  182.         /// <param name="e"></param>
  183.         private void btnCombine_Click(object sender, EventArgs e)
  184.         {
  185.             try
  186.             {
  187.                 if (txtCBlockName.Text.Trim().Equals(string.Empty))
  188.                 {
  189.                     MessageBox.Show(this, "请输入分块文件的扩展名。", "【分块扩展名】不能为空", MessageBoxButtons.OK, MessageBoxIcon.Error);
  190.                     txtCBlockName.Focus();
  191.                     return;
  192.                 }
  193.                 if (txtRename.Text.Trim().Equals(string.Empty))
  194.                 {
  195.                     MessageBox.Show(this, "请输入重命名文件时的后缀。", "【重命名后缀】不能为空", MessageBoxButtons.OK, MessageBoxIcon.Error);
  196.                     txtRename.Focus();
  197.                     return;
  198.                 }
  199.                 pnlCT.Enabled = false;
  200.                 this.Cursor = Cursors.WaitCursor;
  201.                 if (File.Exists(CombineFileName))
  202.                 {
  203.                     FileSpliter fs = new FileSpliter(CombineFileName);
  204.                     fs.ExtName = txtCBlockName.Text;
  205.                     fs.RecogniseChars = txtRename.Text;
  206.                     fs.DeleteBlockFiles = chkDel.Checked;
  207.                     string sInfo = lblCFileInfo.Text;
  208.                     ThreadStart myThreadDelegate = new ThreadStart(fs.Combine);
  209.                     Thread newThread = new Thread(myThreadDelegate);
  210.                     newThread.Start();
  211.                     while (!newThread.IsAlive) ;
  212.                     while (newThread.IsAlive)
  213.                     {
  214.                         Application.DoEvents();
  215.                         pgbCombine.Value = fs.Progress;
  216.                         lblCFileInfo.Text = sInfo + "/n正在合并,已完成" + pgbCombine.Value + "%";
  217.                     }
  218.                     newThread.Join();
  219.                     lblCFileInfo.Text = sInfo + "/n合并成功!生成文件:" + fs.Destination;
  220.                 }
  221.             }
  222.             catch (Exception ex)
  223.             {
  224.                 MessageBox.Show(ex.Message);
  225.             }
  226.             finally
  227.             {
  228.                 pnlCT.Enabled = true;
  229.                 this.Cursor = Cursors.Default;
  230.             }
  231.         }
  232.         private void Link_Clik(object sender, LinkLabelLinkClickedEventArgs e)
  233.         {
  234.             try
  235.             {
  236.                 e.Link.Visited = true;
  237.                 System.Diagnostics.Process.Start(e.Link.LinkData.ToString());
  238.             }
  239.             catch (Exception ex)
  240.             {
  241.                 MessageBox.Show(ex.Message);
  242.             }
  243.         }
  244.     }
  245. }

FileSpliter类:

  1. using System;
  2. using System.IO;
  3. using System.Threading;
  4. namespace 文件分割器
  5.     public class FileSpliter 
  6.     { 
  7.         private int progress; 
  8.         private string lFileName; 
  9.         private string extname = "part"; 
  10.         private string recchars = "_C"; 
  11.         private int blocksize = 1024; 
  12.         private bool pDeleteAfterCombine = false;
  13.         private string ErrorMessage = "";
  14.         private int Part_Size;
  15.         private int mBlockCount;
  16.         private string lDesFileName = "";
  17.         /// <summary>
  18.         /// 构造函数
  19.         /// </summary>
  20.         public FileSpliter()
  21.         {
  22.         }
  23.         /// <summary> 
  24.         /// 构造函数 
  25.         /// </summary> 
  26.         /// <param name="pFileName">设定欲分割或者合并的文件名</param> 
  27.         public FileSpliter(string pFileName)
  28.         {
  29.             this.lFileName = pFileName;
  30.         } 
  31.         /// <summary> 
  32.         /// 获取或者设定要分割/合并的文件名,包含完整路径和文件名。 
  33.         /// </summary> 
  34.         public string FileName 
  35.         { 
  36.             get { return lFileName; } 
  37.             set { lFileName = value; } 
  38.         }
  39.         /// <summary> 
  40.         /// 获取合并后的目标文件名。
  41.         /// </summary> 
  42.         public string Destination
  43.         {
  44.             get { return lDesFileName; }
  45.         }
  46.         
  47.         /// <summary> 
  48.         /// 获取或者设定分块文件的扩展名,默认值:part(即分割后的分块文件将以*.part1,*.part2.....命名) 
  49.         /// </summary> 
  50.         public string ExtName 
  51.         { 
  52.             get { return extname; } 
  53.             set { extname = value; } 
  54.         } 
  55.         /// <summary> 
  56.         /// 获取或者设定合并后是否删除源文件。(true:删除,false:不删除) 
  57.         /// </summary> 
  58.         public bool DeleteBlockFiles 
  59.         { 
  60.             get { return pDeleteAfterCombine; } 
  61.             set { pDeleteAfterCombine = value; } 
  62.         } 
  63.         /// <summary> 
  64.         /// 获取或设定分块文件大小,默认值 1024
  65.         /// </summary> 
  66.         public int BlockSize 
  67.         { 
  68.             get { return blocksize; } 
  69.             set { blocksize = value; } 
  70.         }
  71.         /// <summary> 
  72.         /// 获取分块数
  73.         /// </summary> 
  74.         public int BlockCount
  75.         {
  76.             get { return mBlockCount; }
  77.         }
  78.         
  79.         /// <summary> 
  80.         /// 重命名文件标识字符(默认值:_C)
  81.         /// </summary> 
  82.         public string RecogniseChars 
  83.         { 
  84.             get { return recchars; } 
  85.             set { recchars = value; } 
  86.         }
  87.         /// <summary>
  88.         /// 获取错误信息
  89.         /// </summary>
  90.         public string ErrMsg
  91.         {
  92.             get { return ErrorMessage.Trim(); }            
  93.         }
  94.         /// <summary>
  95.         /// 获取处理进度(0-100)
  96.         /// </summary>
  97.         public int Progress
  98.         {
  99.             get { return progress; }
  100.         }
  101.         /// <summary> 
  102.         /// 将2个byte型变量合并到一个int型变量里 
  103.         /// </summary> 
  104.         /// <param name="byteL">第一个byte变量,合并后的高字节</param> 
  105.         /// <param name="byteR">第二个byte变量,合并后的低字节</param> 
  106.         /// <returns></returns> 
  107.         private int toInt(byte byteL, byte byteR) 
  108.         { 
  109.             return (byteL << 8) + byteR; 
  110.         } 
  111.         /// <summary> 
  112.         /// 把1个int型变量(不超过65535)存到2个byte型变量中
  113.         /// </summary> 
  114.         /// <param name="orgInt">要保存的int变量</param> 
  115.         /// <param name="byteL">第一个byte(存储原int的高字节) 输出参数</param> 
  116.         /// <param name="byteR">第二个byte(存储原int的低字节) 输出参数</param> 
  117.         /// <returns></returns> 
  118.         private int toByte(int orgInt, out byte byteL, out byte byteR) 
  119.         { 
  120.             if (orgInt > 65535) 
  121.             { 
  122.                 throw (new System.ArgumentOutOfRangeException("BlockSize", BlockSize, "分块数超过 65535,请重新定义分块单位大小(BlockSize)")); 
  123.             } 
  124.             byteL = (byte)(orgInt >> 8); 
  125.             byteR = (byte)((orgInt) << 8 >> 8); 
  126.             return 0; 
  127.         } 
  128.         
  129.         /// <summary>
  130.         /// 开始分割
  131.         /// </summary>
  132.         public void Split() 
  133.         { 
  134.             //ThreadStart myThreadDelegate = new ThreadStart(this.pSplit); 
  135.             //Thread newThread = new Thread(myThreadDelegate); 
  136.             //newThread.Start();
  137.             this.pSplit();
  138.         } 
  139.         /// <summary>
  140.         /// 开始合并
  141.         /// </summary>
  142.         public void Combine() 
  143.         { 
  144.             //ThreadStart myThreadDelegate = new ThreadStart(this.pCombine); 
  145.             //Thread newThread = new Thread(myThreadDelegate); 
  146.             //newThread.Start();
  147.             this.pCombine();
  148.         } 
  149.         /// <summary> 
  150.         /// 分割文件. 
  151.         /// </summary> 
  152.         /// <returns></returns> 
  153.         private void pSplit() 
  154.         { 
  155.             try 
  156.             { 
  157.                 if (File.Exists(lFileName)) 
  158.                 { 
  159.                     int i = 1; 
  160.                     string newFileName = lFileName + "." + extname; 
  161.                     FileInfo fi = new FileInfo(lFileName); 
  162.                     filestream fs = fi.OpenRead(); 
  163.                     Part_Size = blocksize * 1024; 
  164.                     long lPartCnt = (fs.Length + 1) / Part_Size + 1; 
  165.                     if (lPartCnt > 65535) 
  166.                     { 
  167.                         throw (new System.ArgumentOutOfRangeException("BlockSize", BlockSize, "The block files is much than 65535,need redefine BlockSize")); 
  168.                     }
  169.                     mBlockCount = (int)lPartCnt; 
  170.                     byte[] ByteArray = new byte[Part_Size];
  171.                     toByte(mBlockCount, out ByteArray[0], out ByteArray[1]); 
  172.                     int nBytesRead = fs.Read(ByteArray, 2, Part_Size - 2); 
  173.                     byte[] part = new byte[nBytesRead + 2]; 
  174.                     Array.Copy(ByteArray, 0, part, 0, part.Length); 
  175.                     FileInfo fo = new FileInfo(newFileName + i++.ToString()); 
  176.                     FileStream fso = fo.OpenWrite(); 
  177.                     fso.Write(part, 0, part.Length); 
  178.                     fso.Close(); 
  179.                     fso.Dispose();
  180.                     progress = (int)((1.0 / mBlockCount) * 100); 
  181.                     while (part.Length == Part_Size) 
  182.                     { 
  183.                         nBytesRead = fs.Read(ByteArray, 0, Part_Size); 
  184.                         if (nBytesRead > 0) 
  185.                         { 
  186.                             part = new byte[nBytesRead]; 
  187.                             Array.Copy(ByteArray, 0, part, 0, nBytesRead); 
  188.                             fo = new FileInfo(newFileName + i.ToString()); 
  189.                             fso = fo.OpenWrite(); 
  190.                             fso.Write(part, 0, part.Length); 
  191.                             fso.Close(); 
  192.                             fso.Dispose();
  193.                             progress = (int)((i++ * 1.0 / mBlockCount) * 100); 
  194.                         } 
  195.                     } 
  196.                     fs.Close(); 
  197.                     fs.Dispose(); 
  198.                 } 
  199.                 else 
  200.                 { 
  201.                     throw (new ArgumentNullException("FileName", "FileName not set or the file doesnot exist.")); 
  202.                 } 
  203.             } 
  204.             catch (Exception ex) 
  205.             { 
  206.                 throw (ex); 
  207.             } 
  208.         }
  209.         /// <summary>
  210.         /// 检查所有分块文件是否完整
  211.         /// </summary>
  212.         public void CheckBlockEntire()
  213.         {
  214.             try
  215.             {
  216.                 FileInfo fi = new FileInfo(lFileName);
  217.                 FileStream fsi = fi.OpenRead();
  218.                 byte[] part = new byte[2];
  219.                 string errmsg = "缺少文件:";
  220.                 int errcnt = 0;
  221.                 fsi.Read(part, 0, 2);
  222.                 fsi.Close();
  223.                 fsi.Dispose();
  224.                 mBlockCount = toInt(part[0], part[1]);
  225.                 progress = 1;
  226.                 for (int i = 2; i <= mBlockCount; i++)
  227.                 {
  228.                     if (!File.Exists(lFileName.substring(0, lFileName.Length - 1) + i))
  229.                     {
  230.                         errmsg += " " + lFileName.Substring(0, lFileName.Length - 1) + i;
  231.                         errcnt++;
  232.                     }
  233.                     progress = (int)(i * 1.0 / mBlockCount) * 100;
  234.                 }
  235.                 if (errcnt > 0)
  236.                 {
  237.                     ErrorMessage = errmsg;
  238.                 }
  239.                 else
  240.                 {
  241.                     ErrorMessage = "";
  242.                 }                
  243.             }
  244.             catch (Exception ex)
  245.             {
  246.                 ErrorMessage = ex.Message;
  247.             }
  248.         }
  249.         /// <summary>
  250.         /// 合并指定分块文件
  251.         /// </summary>
  252.         private void pCombine() 
  253.         { 
  254.             try 
  255.             { 
  256.                 if (File.Exists(lFileName)) 
  257.                 { 
  258.                     int i; 
  259.                     string NewFn = lFileName.Substring(0, lFileName.Lastindexof("." + extname)); 
  260.                     if (File.Exists(NewFn)) 
  261.                     { 
  262.                         string sNameX = NewFn.Substring(0, NewFn.LastIndexOf(".")); 
  263.                         string sExtName = NewFn.Substring(sNameX.Length); 
  264.                         NewFn = sNameX + recchars + sExtName; 
  265.                         i = 1; 
  266.                         while (File.Exists(NewFn)) 
  267.                         { 
  268.                             NewFn = sNameX + recchars + i++ + sExtName; 
  269.                         } 
  270.                     } 
  271.                     FileInfo fi = new FileInfo(lFileName); 
  272.                     FileStream fsi = fi.OpenRead(); 
  273.                     byte[] part = new byte[fsi.Length]; 
  274.                     fsi.Read(part, 0, part.Length); 
  275.                     fsi.Close(); 
  276.                     fsi.Dispose(); 
  277.                     int FileCnt = toInt(part[0], part[1]);
  278.                     progress = 0;
  279.                     for (i = 2; i <= FileCnt; i++) 
  280.                     { 
  281.                         if (!File.Exists(lFileName.Substring(0, lFileName.Length - 1) + i)) 
  282.                         { 
  283.                             throw (new System.ArgumentNullException("FileName", "缺少文件:" + lFileName.Substring(0, lFileName.Length - 1) + i));                             
  284.                         } 
  285.                     } 
  286.                     FileInfo fo = new FileInfo(NewFn); 
  287.                     FileStream fso = fo.OpenWrite();
  288.                     lDesFileName = NewFn;
  289.                     fso.Write(part, 2, part.Length - 2);
  290.                     progress = (int)((1.0 / FileCnt) * 100);
  291.                     for (i = 2; i <= FileCnt; i++) 
  292.                     { 
  293.                         fi = new FileInfo(lFileName.Substring(0, lFileName.Length - 1) + i); 
  294.                         fsi = fi.OpenRead(); 
  295.                         part = new byte[fsi.Length]; 
  296.                         fsi.Read(part, 0, part.Length); 
  297.                         fsi.Close(); 
  298.                         fsi.Dispose(); 
  299.                         fso.Write(part, 0, part.Length);
  300.                         progress = (int)((i * 1.0 / FileCnt) * 100);
  301.                     } 
  302.                     fso.Close(); 
  303.                     fso.Dispose(); 
  304.                     if (pDeleteAfterCombine) 
  305.                     { 
  306.                         for (i = 1; i <= FileCnt; i++) 
  307.                         { 
  308.                             File.Delete(lFileName.Substring(0, lFileName.Length - 1) + i); 
  309.                         } 
  310.                     }
  311.                 } 
  312.                 else 
  313.                 { 
  314.                     throw (new ArgumentNullException("FileName", "FileName not set or the file doesnot exist.")); 
  315.                 } 
  316.             } 
  317.             catch (Exception ex) 
  318.             {
  319.                 ErrorMessage = ex.Message;
  320.             } 
  321.         } 
  322.     } 

相关阅读

操作系统总结之文件管理

一:主要内容: 概述 文件的逻辑结构 ( 顺序文件,索引文件,索引顺序文件,直接文件和哈希文件 ) 外存分配方式 文件目录管理 文件存储

详述 hosts 文件的作用及修改 hosts 文件的方法

1 什么是hosts文件? hosts是一个没有扩展名的系统文件,其基本作用就是将一些常用的网址域名与其对应的 IP 地址建立一个关联“ 数

AVI音视频封装格式学习(二)——AVI RIFF文件参考

AVI RIFF文件参考 AVI RIFF File Reference   微软AVI文件格式是与捕获,编辑和播放音视频流的应用程序一起使用的RIFF文件规范。

linux中修改文件名

mv 想改变的文件 改变以后的文件 mv test.js template.js

教你用迅雷下载百度云网盘的大文件

相信大家都会在百度网盘下载文件,但是可恶的百度限制了百度云管家的下载速度,要想下载速度块就得掏钱办会员。今天发现一个方法,可以

分享到:

栏目导航

推荐阅读

热门阅读