组合时采用了两层的COPY命令,可多组合一些文件,其实用测试命令行总长度的办法可以理论上实现无限拆分文件的组合,但实用价值就不高了,拆成万余份文件不但此单线程方法显得效率低下,而且应当用更优秀算法进行分割和组合。
这个程序最终只能归为“玩具”一类。
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace filesplit
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.OpenFileDialog ofd;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.ofd = new System.Windows.Forms.OpenFileDialog();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.textBox1.Location = new System.Drawing.Point(16, 16);
this.textBox1.Name = "textBox1";
this.textBox1.ReadOnly = true;
this.textBox1.Size = new System.Drawing.Size(376, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "File Path";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(56, 48);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(72, 21);
this.textBox2.TabIndex = 1;
this.textBox2.Text = "";
//
// button1
//
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.button1.Location = new System.Drawing.Point(200, 48);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(272, 24);
this.button1.TabIndex = 2;
this.button1.Text = "拆分";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.button2.Location = new System.Drawing.Point(400, 16);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(72, 24);
this.button2.TabIndex = 3;
this.button2.Text = "浏览";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 48);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(32, 23);
this.label1.TabIndex = 4;
this.label1.Text = "尺寸";
//
// label2
//
this.label2.Location = new System.Drawing.Point(136, 48);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(56, 16);
this.label2.TabIndex = 5;
this.label2.Text = "KB";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(498, 79);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Form1";
this.Text = "Spliter";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button2_Click(object sender, System.EventArgs e)
{
if(this.ofd.ShowDialog()!=System.Windows.Forms.DialogResult.OK){return;}
this.textBox1.Text=ofd.FileName;
}
private void button1_Click(object sender, System.EventArgs e)
{
try
{
int i=1;
try
{
i=int.Parse(this.textBox2.Text);
if (i <= 0) { System.Exception ee = new Exception(); throw ee; }
i*=1000;
}
catch
{
i=0;
System.Windows.Forms.MessageBox.Show("输入拆分数字不正确");
return;
}
#region "分配文件夹"
int count=1;
string workpath=ofd.FileName.ToString()+" Split";
while(true)
{
if(!System.IO.Directory.Exists(workpath)){break;}
else
{
if(!System.IO.Directory.Exists(workpath+"_"+count.ToString()))
{
workpath+="_"+count.ToString();
break;
}
else
{
count++;
}
}
}
System.IO.Directory.CreateDirectory(workpath);
#endregion
System.IO.FileStream f=new System.IO.FileStream(ofd.FileName,System.IO.FileMode.Open);
System.IO.BinaryReader r = new System.IO.BinaryReader(f);
long l=f.Length;long x=0;
int filecount=1;
for(;x<l;)
{
System.IO.FileStream f2=new System.IO.FileStream(workpath+"\\"+filecount.ToString(),System.IO.FileMode.CreateNew);
for(int for1=0;for1<i;for1++)
{
if(x<l)
{
f2.WriteByte(r.ReadByte());
}
x++;
}
f2.Close();
filecount++;
}
f.Close();
System.IO.StreamWriter t =new System.IO.StreamWriter(System.IO.File.Open(workpath+"\\"+"组合文件.bat",System.IO.FileMode.Create),System.Text.Encoding.Default);
t.WriteLine("@echo off");
t.Write("copy ");
int tempcount = 0;
for(int for2=1;for2<filecount;for2++)
{
t.Write(for2.ToString()+" /b");
if(for2+1!=filecount)
{
if ((for2 % 100) == 0)
{
t.Write(" " + "temp" + ((int)(tempcount++)).ToString());
t.WriteLine();
t.Write("copy ");
}
else
{
t.Write(" + ");
}
}
else{
t.Write(" " + "temp" + ((int)(tempcount++)).ToString());
t.WriteLine();
}
}
t.Write("copy ");
for (int for2 = 0; for2 < tempcount; for2++)
{
t.Write("temp" + for2.ToString() + " /b");
if (for2 + 1 != tempcount && tempcount!=0)
{
t.Write(" + ");
}
else{
t.Write(" "+getfilename(ofd.FileName));
t.WriteLine();
}
}
for (int for2 = 0; for2 < tempcount; for2++)
{
t.WriteLine("del " + "temp" + for2.ToString());
}
for(int for2=1;for2<filecount;for2++)
{
t.WriteLine("del "+for2);
}
t.Close();
filecount-=1;
System.Windows.Forms.MessageBox.Show("拆分文件创建已经完成,共拆分为"+filecount.ToString()+"部分");
}
catch(System.Exception ee)
{
System.Windows.Forms.MessageBox.Show("操作出现失误,没有完成拆分:"+ee.Message );
GC.Collect();
}
}
private string getfilename(string str)
{
int firstname=0;
string result="";
for(int i=str.Length-1;i>=0;i--)
{
if(str[i]=='\\')
{
firstname=i;
break;
}
}
for(int i=firstname+1;i<str.Length;i++)
{
result+=str[i];
}
return result;
}
}
}
分享到:
相关推荐
Txt文件拆分 把一个大的txt文件拆分成小的txt文件,拆分时可以自动转换文件编码, 主要用于电子书的生成 为同学做的他的要为sbk文件,还有有大小限制,还要转换为Unicode的编码形式,太麻烦了.自己做了一个!...
基于C#写的文件拆分和合并处理程序,源码和应用都有,注释详细,已通过检测成功,分割合并文本文档无错误。
用C#做的一个拆分器,可以把文件进行拆分
读取服务器下载的EXCEl文件,报错:外部表不是预期的格式。进行格式转换后,生成新的Excel文件,根据某一列进行分组,并把分组后的结果填充到固定的Excel文件格式中。因为是用自动化测试工具写的,里面有一些是测试...
用c#2008编译的文件拆分和合并。其中浏览是指要拆分的word或图片等文件,拆分名字是指拆分后的名字以什么开头,保存是指拆分后的文件路径,合并名字必须要输入合并后的格式如.tex或.word。
C#超大文件分割工具,可对任意文件(按指定数目、指定大小)进行分割拆解 ,以便于移动、复制 和 存储。 一键分割、一键合并。 C#超大文件分割工具,可对任意文件(按指定数目、指定大小)进行分割拆解 ,以便于...
把大的PDF文件拆分成指定大小文件,但是因为每页的文件大小不一定,就不能通过固定页数来拆分文件,这样子的话就需要我们通过计算来将文件拆分这指定大小的文件
将pdf按页拆分成多个小的pdf,并可将pdf转换成jpeg图片,同时具有OCR文字识别功能,可提取pdf和图片中的内容,内置图片识别区域设置。
PDF文件拆分、合并工具,带C#代码,调用iTextSharp
拆分CSV小程序的源码,注释应该就不必了,比较简单,可以搞定百万条的数据,再大的理论上也可以,内存占用比较少。
以VS2008工具写的,对Excel文件的合并、拆分处理。
主要为大家详细介绍了C#合并及拆分PDF文件的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
C#企业培训第十三颗课源码(文件拆分工具) 给大家做参考用的,请关注更新 实现基本功能,可能效率上有问题,没有用多线程
将多个文件保存为一个文件并将文件拆分开来;即合并文件;组合多个文件为一个文件
winForm程序选择tiff文件后,对tiff文件进行拆分,将拆分后的矩阵展示在窗口中,可以拆分多个tiff文件展示在窗口,点击合并后,将所有展示的图片合并成新的tiff文件并输出到本地
文件拆分器 很有用哦 可以用来学习 也可以自己试试用哦
C#利用log4net写入日志文件
使用.net(C#)做的文件切分器,实现文件的切分和和并
资源里加入了拆分后的图片的质量控制,IntPtr类型的 ptr变量为全局变量,就是它保存着质量控制信息,主方法 名为getThreePicture 返回值是string[]保存着三张拆分后的图片的base64格式字符串信息,主方法前有unsafe...
将一个Word文档分割成多个文档,并且保留源文档格式不变。 操作原理是,打开需要操作的文档,把不需要的页面删除掉, 然后另存为文档,如此反复操作就可取出所有页数。 项目已编译好可直接运行...开发环境:vs2017 c#