文件分割器
程序预览:
窗体代码:
- using System;
- using System.Collections.Generic;
- using System.componentModel;
- using System.Data;
- using System.Drawing;
- //using System.Linq;
- using System.Text;
- using System.windows.Forms;
- using System.IO;
- using System.Threading;
- namespace 文件分割器
- {
- public partial class fmMain : Form
- {
- private string SplitFileName ="";
- private string CombineFileName = "";
- public fmMain()
- {
- InitializeComponent();
- }
- /// <summary>
- /// 选择要分割的文件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnSSelect_Click(object sender, EventArgs e)
- {
- try
- {
- OpenFileDialog ofd = new OpenFileDialog();
- ofd.Filter = "所有文件(*.*)|*.*";
- pgbSplit.Value = 0;
- if (ofd.ShowDialog().Equals(dialogresult.OK))
- {
- SplitFileName = ofd.FileName;
- FileInfo fi = new FileInfo(SplitFileName);
- lblSFileInfo.Text = "文件信息:/n" +
- "文件名:" + fi.Name + "/n" +
- "路径:" + fi.DirectoryName + "/n" +
- "大小:" + fi.Length.ToString("#,###") + "Bytes(" + (fi.Length/1024/1024).ToString("#,###") + "MB)/n" +
- "属性:" + (fi.IsReadOnly ? "只读/n" : "/n") +
- "创建时间:" + fi.CreationTime.ToLongDateString() + " " + fi.CreationTime.ToLongTimeString();
- btnSplit.Enabled = true;
- }
- ofd.Dispose();
- }
- catch (Exception ex)
- {
- messageBox.Show(ex.Message);
- }
- }
- /// <summary>
- /// 初始化
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void fmMain_Load(object sender, EventArgs e)
- {
- try
- {
- btnSplit.Enabled = false;
- lblAbout.Links.Add(113, 24, "mailto:Vicoman(MSN Email)<[email protected]>");
- lblAbout.Links.Add(146, 15, "mailto:Vicoman<[email protected]>");
- lblAbout.Links.Add(174, 28, "http://blog.csdn.net/vicoman");
- }
- catch(Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- /// <summary>
- /// 分割选定文件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnSplit_Click(object sender, EventArgs e)
- {
- try
- {
- if (txtSBlockName.Text.Trim().Equals(string.empty))
- {
- MessageBox.Show(this,"请输入分块文件的扩展名。", "【分块扩展名】不能为空", MessageBoxButtons.OK, MessageBoxIcon.ERROR);
- txtSBlockName.Focus();
- return;
- }
- if (cmbSize.Text.Trim().Equals(string.Empty))
- {
- MessageBox.Show(this, "请输入分块文件的大小。", "【分块大小】不能为空", MessageBoxButtons.OK, MessageBoxIcon.Error);
- cmbSize.Focus();
- return;
- }
- pnlST.Enabled = false;
- this.Cursor = Cursors.WaitCursor;
- if (File.exists(SplitFileName))
- {
- FileSpliter fs = new FileSpliter(SplitFileName);
- fs.ExtName = txtSBlockName.Text.Trim();
- fs.BlockSize = int.Parse((cmbSize.Text.Replace(",", "")));
- string sInfo = lblSFileInfo.Text;
- ThreadStart myThreadDelegate = new ThreadStart(fs.Split);
- Thread newThread = new Thread(myThreadDelegate);
- newThread.Start();
- while (!newThread.IsAlive);
- while (newThread.IsAlive)
- {
- APPlication.DoEvents();
- pgbSplit.Value=fs.progress;
- lblSFileInfo.Text = sInfo + "/n正在分割,已完成" + pgbSplit.Value + "%";
- }
- newthread.join();
- lblSFileInfo.Text = sInfo + "/n分割完成!分块数:" + fs.BlockCount;
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- finally
- {
- pnlST.Enabled = true;
- this.Cursor = Cursors.Default;
- }
- }
- /// <summary>
- /// 选择合并文件的第一分块,并检查所有分块的完整性
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnCSelect_Click(object sender, EventArgs e)
- {
- try
- {
- OpenFileDialog ofd = new OpenFileDialog();
- ofd.Filter = "第一个分块文件(*." + txtCBlockName.Text + "1)|*." + txtCBlockName.Text + "1";
- if (ofd.ShowDialog().Equals(DialogResult.OK))
- {
- CombineFileName = ofd.FileName;
- lblCFileInfo.Text = "正在检查分块文件完整性......";
- FileSpliter fs = new FileSpliter(CombineFileName);
- ThreadStart myThreadDelegate = new ThreadStart(fs.CheckBlockEntire);
- Thread newThread = new Thread(myThreadDelegate);
- newThread.Start();
- while (!newThread.IsAlive) ;
- while (newThread.IsAlive)
- {
- Application.DoEvents();
- pgbCombine.Value = fs.Progress;
- }
- newThread.Join();
- FileInfo fi = new FileInfo(CombineFileName);
- string sInfo = "文件信息:/n" +
- "文件名:" + fi.Name + "/n" +
- "路径:" + fi.DirectoryName + "/n" +
- "大小:" + fi.Length.ToString("#,###") + "Bytes(" +
- (fi.Length / 1024 / 1024).ToString("#,###") + "MB)/n" +
- "创建时间:" + fi.CreationTime.ToLongDateString() + " " +
- fi.CreationTime.ToLongTimeString();
- if (fs.ErrMsg.Length > 0)
- {
- lblCFileInfo.Text = sInfo + fs.ErrMsg;
- btnCombine.Enabled = false;
- }
- else
- {
- lblCFileInfo.Text = sInfo + "/n分块文件完整性检查成功,分块数:" +
- fs.BlockCount + ",请点击/"开始合并/"进行合并。";
- btnCombine.Enabled = true;
- }
- pgbCombine.Value = 0;
- }
- ofd.Dispose();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- /// <summary>
- /// 合并所选文件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnCombine_Click(object sender, EventArgs e)
- {
- try
- {
- if (txtCBlockName.Text.Trim().Equals(string.Empty))
- {
- MessageBox.Show(this, "请输入分块文件的扩展名。", "【分块扩展名】不能为空", MessageBoxButtons.OK, MessageBoxIcon.Error);
- txtCBlockName.Focus();
- return;
- }
- if (txtRename.Text.Trim().Equals(string.Empty))
- {
- MessageBox.Show(this, "请输入重命名文件时的后缀。", "【重命名后缀】不能为空", MessageBoxButtons.OK, MessageBoxIcon.Error);
- txtRename.Focus();
- return;
- }
- pnlCT.Enabled = false;
- this.Cursor = Cursors.WaitCursor;
- if (File.Exists(CombineFileName))
- {
- FileSpliter fs = new FileSpliter(CombineFileName);
- fs.ExtName = txtCBlockName.Text;
- fs.RecogniseChars = txtRename.Text;
- fs.DeleteBlockFiles = chkDel.Checked;
- string sInfo = lblCFileInfo.Text;
- ThreadStart myThreadDelegate = new ThreadStart(fs.Combine);
- Thread newThread = new Thread(myThreadDelegate);
- newThread.Start();
- while (!newThread.IsAlive) ;
- while (newThread.IsAlive)
- {
- Application.DoEvents();
- pgbCombine.Value = fs.Progress;
- lblCFileInfo.Text = sInfo + "/n正在合并,已完成" + pgbCombine.Value + "%";
- }
- newThread.Join();
- lblCFileInfo.Text = sInfo + "/n合并成功!生成文件:" + fs.Destination;
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- finally
- {
- pnlCT.Enabled = true;
- this.Cursor = Cursors.Default;
- }
- }
- private void Link_Clik(object sender, LinkLabelLinkClickedEventArgs e)
- {
- try
- {
- e.Link.Visited = true;
- System.Diagnostics.Process.Start(e.Link.LinkData.ToString());
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- }
- }
- using System;
- using System.IO;
- using System.Threading;
- namespace 文件分割器
- {
- public class FileSpliter
- {
- private int progress;
- private string lFileName;
- private string extname = "part";
- private string recchars = "_C";
- private int blocksize = 1024;
- private bool pDeleteAfterCombine = false;
- private string ErrorMessage = "";
- private int Part_Size;
- private int mBlockCount;
- private string lDesFileName = "";
- /// <summary>
- /// 构造函数
- /// </summary>
- public FileSpliter()
- {
- }
- /// <summary>
- /// 构造函数
- /// </summary>
- /// <param name="pFileName">设定欲分割或者合并的文件名</param>
- public FileSpliter(string pFileName)
- {
- this.lFileName = pFileName;
- }
- /// <summary>
- /// 获取或者设定要分割/合并的文件名,包含完整路径和文件名。
- /// </summary>
- public string FileName
- {
- get { return lFileName; }
- set { lFileName = value; }
- }
- /// <summary>
- /// 获取合并后的目标文件名。
- /// </summary>
- public string Destination
- {
- get { return lDesFileName; }
- }
- /// <summary>
- /// 获取或者设定分块文件的扩展名,默认值:part(即分割后的分块文件将以*.part1,*.part2.....命名)
- /// </summary>
- public string ExtName
- {
- get { return extname; }
- set { extname = value; }
- }
- /// <summary>
- /// 获取或者设定合并后是否删除源文件。(true:删除,false:不删除)
- /// </summary>
- public bool DeleteBlockFiles
- {
- get { return pDeleteAfterCombine; }
- set { pDeleteAfterCombine = value; }
- }
- /// <summary>
- /// 获取或设定分块文件大小,默认值 1024
- /// </summary>
- public int BlockSize
- {
- get { return blocksize; }
- set { blocksize = value; }
- }
- /// <summary>
- /// 获取分块数
- /// </summary>
- public int BlockCount
- {
- get { return mBlockCount; }
- }
- /// <summary>
- /// 重命名文件标识字符(默认值:_C)
- /// </summary>
- public string RecogniseChars
- {
- get { return recchars; }
- set { recchars = value; }
- }
- /// <summary>
- /// 获取错误信息
- /// </summary>
- public string ErrMsg
- {
- get { return ErrorMessage.Trim(); }
- }
- /// <summary>
- /// 获取处理进度(0-100)
- /// </summary>
- public int Progress
- {
- get { return progress; }
- }
- /// <summary>
- /// 将2个byte型变量合并到一个int型变量里
- /// </summary>
- /// <param name="byteL">第一个byte变量,合并后的高字节</param>
- /// <param name="byteR">第二个byte变量,合并后的低字节</param>
- /// <returns></returns>
- private int toInt(byte byteL, byte byteR)
- {
- return (byteL << 8) + byteR;
- }
- /// <summary>
- /// 把1个int型变量(不超过65535)存到2个byte型变量中
- /// </summary>
- /// <param name="orgInt">要保存的int变量</param>
- /// <param name="byteL">第一个byte(存储原int的高字节) 输出参数</param>
- /// <param name="byteR">第二个byte(存储原int的低字节) 输出参数</param>
- /// <returns></returns>
- private int toByte(int orgInt, out byte byteL, out byte byteR)
- {
- if (orgInt > 65535)
- {
- throw (new System.ArgumentOutOfRangeException("BlockSize", BlockSize, "分块数超过 65535,请重新定义分块单位大小(BlockSize)"));
- }
- byteL = (byte)(orgInt >> 8);
- byteR = (byte)((orgInt) << 8 >> 8);
- return 0;
- }
- /// <summary>
- /// 开始分割
- /// </summary>
- public void Split()
- {
- //ThreadStart myThreadDelegate = new ThreadStart(this.pSplit);
- //Thread newThread = new Thread(myThreadDelegate);
- //newThread.Start();
- this.pSplit();
- }
- /// <summary>
- /// 开始合并
- /// </summary>
- public void Combine()
- {
- //ThreadStart myThreadDelegate = new ThreadStart(this.pCombine);
- //Thread newThread = new Thread(myThreadDelegate);
- //newThread.Start();
- this.pCombine();
- }
- /// <summary>
- /// 分割文件.
- /// </summary>
- /// <returns></returns>
- private void pSplit()
- {
- try
- {
- if (File.Exists(lFileName))
- {
- int i = 1;
- string newFileName = lFileName + "." + extname;
- FileInfo fi = new FileInfo(lFileName);
- filestream fs = fi.OpenRead();
- Part_Size = blocksize * 1024;
- long lPartCnt = (fs.Length + 1) / Part_Size + 1;
- if (lPartCnt > 65535)
- {
- throw (new System.ArgumentOutOfRangeException("BlockSize", BlockSize, "The block files is much than 65535,need redefine BlockSize"));
- }
- mBlockCount = (int)lPartCnt;
- byte[] ByteArray = new byte[Part_Size];
- toByte(mBlockCount, out ByteArray[0], out ByteArray[1]);
- int nBytesRead = fs.Read(ByteArray, 2, Part_Size - 2);
- byte[] part = new byte[nBytesRead + 2];
- Array.Copy(ByteArray, 0, part, 0, part.Length);
- FileInfo fo = new FileInfo(newFileName + i++.ToString());
- FileStream fso = fo.OpenWrite();
- fso.Write(part, 0, part.Length);
- fso.Close();
- fso.Dispose();
- progress = (int)((1.0 / mBlockCount) * 100);
- while (part.Length == Part_Size)
- {
- nBytesRead = fs.Read(ByteArray, 0, Part_Size);
- if (nBytesRead > 0)
- {
- part = new byte[nBytesRead];
- Array.Copy(ByteArray, 0, part, 0, nBytesRead);
- fo = new FileInfo(newFileName + i.ToString());
- fso = fo.OpenWrite();
- fso.Write(part, 0, part.Length);
- fso.Close();
- fso.Dispose();
- progress = (int)((i++ * 1.0 / mBlockCount) * 100);
- }
- }
- fs.Close();
- fs.Dispose();
- }
- else
- {
- throw (new ArgumentNullException("FileName", "FileName not set or the file doesnot exist."));
- }
- }
- catch (Exception ex)
- {
- throw (ex);
- }
- }
- /// <summary>
- /// 检查所有分块文件是否完整
- /// </summary>
- public void CheckBlockEntire()
- {
- try
- {
- FileInfo fi = new FileInfo(lFileName);
- FileStream fsi = fi.OpenRead();
- byte[] part = new byte[2];
- string errmsg = "缺少文件:";
- int errcnt = 0;
- fsi.Read(part, 0, 2);
- fsi.Close();
- fsi.Dispose();
- mBlockCount = toInt(part[0], part[1]);
- progress = 1;
- for (int i = 2; i <= mBlockCount; i++)
- {
- if (!File.Exists(lFileName.substring(0, lFileName.Length - 1) + i))
- {
- errmsg += " " + lFileName.Substring(0, lFileName.Length - 1) + i;
- errcnt++;
- }
- progress = (int)(i * 1.0 / mBlockCount) * 100;
- }
- if (errcnt > 0)
- {
- ErrorMessage = errmsg;
- }
- else
- {
- ErrorMessage = "";
- }
- }
- catch (Exception ex)
- {
- ErrorMessage = ex.Message;
- }
- }
- /// <summary>
- /// 合并指定分块文件
- /// </summary>
- private void pCombine()
- {
- try
- {
- if (File.Exists(lFileName))
- {
- int i;
- string NewFn = lFileName.Substring(0, lFileName.Lastindexof("." + extname));
- if (File.Exists(NewFn))
- {
- string sNameX = NewFn.Substring(0, NewFn.LastIndexOf("."));
- string sExtName = NewFn.Substring(sNameX.Length);
- NewFn = sNameX + recchars + sExtName;
- i = 1;
- while (File.Exists(NewFn))
- {
- NewFn = sNameX + recchars + i++ + sExtName;
- }
- }
- FileInfo fi = new FileInfo(lFileName);
- FileStream fsi = fi.OpenRead();
- byte[] part = new byte[fsi.Length];
- fsi.Read(part, 0, part.Length);
- fsi.Close();
- fsi.Dispose();
- int FileCnt = toInt(part[0], part[1]);
- progress = 0;
- for (i = 2; i <= FileCnt; i++)
- {
- if (!File.Exists(lFileName.Substring(0, lFileName.Length - 1) + i))
- {
- throw (new System.ArgumentNullException("FileName", "缺少文件:" + lFileName.Substring(0, lFileName.Length - 1) + i));
- }
- }
- FileInfo fo = new FileInfo(NewFn);
- FileStream fso = fo.OpenWrite();
- lDesFileName = NewFn;
- fso.Write(part, 2, part.Length - 2);
- progress = (int)((1.0 / FileCnt) * 100);
- for (i = 2; i <= FileCnt; i++)
- {
- fi = new FileInfo(lFileName.Substring(0, lFileName.Length - 1) + i);
- fsi = fi.OpenRead();
- part = new byte[fsi.Length];
- fsi.Read(part, 0, part.Length);
- fsi.Close();
- fsi.Dispose();
- fso.Write(part, 0, part.Length);
- progress = (int)((i * 1.0 / FileCnt) * 100);
- }
- fso.Close();
- fso.Dispose();
- if (pDeleteAfterCombine)
- {
- for (i = 1; i <= FileCnt; i++)
- {
- File.Delete(lFileName.Substring(0, lFileName.Length - 1) + i);
- }
- }
- }
- else
- {
- throw (new ArgumentNullException("FileName", "FileName not set or the file doesnot exist."));
- }
- }
- catch (Exception ex)
- {
- ErrorMessage = ex.Message;
- }
- }
- }
- }
相关阅读
一:主要内容: 概述 文件的逻辑结构 ( 顺序文件,索引文件,索引顺序文件,直接文件和哈希文件 ) 外存分配方式 文件目录管理 文件存储
1 什么是hosts文件? hosts是一个没有扩展名的系统文件,其基本作用就是将一些常用的网址域名与其对应的 IP 地址建立一个关联“ 数
AVI RIFF文件参考 AVI RIFF File Reference 微软AVI文件格式是与捕获,编辑和播放音视频流的应用程序一起使用的RIFF文件规范。
mv 想改变的文件 改变以后的文件 mv test.js template.js
相信大家都会在百度网盘下载文件,但是可恶的百度限制了百度云管家的下载速度,要想下载速度块就得掏钱办会员。今天发现一个方法,可以