Reader Level:
ARTICLE

File Merger and Splitter in C#

Posted by sayginteh Articles | C# Language April 10, 2001
This program can break up a large file into several files AND merge those files back into one single large file.
  • 0
  • 0
  • 53092
Download Files:
 

This program can be used to :

  1. break up a large file into several files AND
  2. merge those files back into one single large file.

This is particularly useful if there is a file say 2mb, and you want to save it to a floppy disk to transfer to another PC.

Split the 2mb file into several files (you can specify how many files) in the first PC. Then copy those files (a number with extension "sg"). Place all the files in the same directory in the second PC. Use this program to merge those files. The result of merging those files is defaulted to "output.sg". Rename "output.sg" accordingly.


This assumes both PC have .net environment.
Best wishes and good luck !

Source Code

/*
Author : Say Gin Teh
Date : 06/4/2001
Copyright
Free to distribute this program only for educational purpose
This basic program can be used to split a file into many smaller files (eg 1.sg,2.sg,3.sg ...etc).
You can also merge all the smaller files into one file (eg output.sg) - Then you can rename this file to the original file name
*/
//csc /r:System.Drawing.DLL;System.Data.dll;System.WinForms.dll;Microsoft.Win32.Interop.DLL;System.dll SplitFile.cs
using System;
using System.Drawing;
using System.ComponentModel;
using System.WinForms;
using System.IO;
namespace SGSplitFileNamespace
{
/// <summary>
/// Summary description for SplitFile.
/// </summary>
public class SplitFile : System.WinForms.Form
{
/// <summary>
/// Required by the Win Forms designer
/// </summary>
private System.ComponentModel.Container components;
private System.WinForms.Label label4;
private System.WinForms.Button button4;
private System.WinForms.Button button3;
private System.WinForms.TextBox textBox2;
private System.WinForms.Label label3;
private System.WinForms.TextBox textBox1;
private System.WinForms.Label label2;
private System.WinForms.Button button2;
private System.WinForms.Button button1;
private System.WinForms.Label label1;
private System.WinForms.GroupBox groupBox1;
private File f = null; // The file concern
private int x = 1; // number for splits
private long len = 0; // the lengt of the original file
private bool splitFile = false; // splitfile = true, mergefile = false
private int eachSize = 0; // max size of each file
private string outputName = "output.sg"; // the output of merge file
public SplitFile()
{
InitializeComponent();
}
/// <summary>
/// Clean up any resources being used
/// </summary>
public override void Dispose()
{
base.Dispose();
components.Dispose();
}
/// <summary>
/// The main entry point for the application.
/// </summary>
public static void Main(string[] args)
{
Application.Run(
new SplitFile());
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with an editor
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.groupBox1 = new System.WinForms.GroupBox();
this.textBox1 = new System.WinForms.TextBox();
this.label3 = new System.WinForms.Label();
this.label4 = new System.WinForms.Label();
this.label2 = new System.WinForms.Label();
this.button1 = new System.WinForms.Button();
this.textBox2 = new System.WinForms.TextBox();
this.button4 = new System.WinForms.Button();
this.label1 = new System.WinForms.Label();
this.button3 = new System.WinForms.Button();
this.button2 = new System.WinForms.Button();
//@design this.TrayHeight = 0;
//@design this.TrayLargeIcon = false;
//@design this.TrayAutoArrange = true;
groupBox1.Location = new System.Drawing.Point(32, 24);
groupBox1.TabIndex = 1;
groupBox1.TabStop =
false;
groupBox1.Text = "Main";
groupBox1.Size =
new System.Drawing.Size(312, 80);
textBox1.Location =
new System.Drawing.Point(144, 136);
textBox1.TabIndex = 3;
textBox1.Size = new System.Drawing.Size(200, 20);
textBox1.Visible =
false;
label3.Location =
new System.Drawing.Point(32, 176);
label3.Size =
new System.Drawing.Size(88, 28);
label3.TabIndex = 4;
label3.Visible =
false;
label4.Location =
new System.Drawing.Point(32, 216);
label4.Size =
new System.Drawing.Size(312, 24);
label4.TabIndex = 8;
label2.Location =
new System.Drawing.Point(32, 136);
label2.Size =
new System.Drawing.Size(88, 28);
label2.TabIndex = 2;
label2.Visible =
false;
button1.Location =
new System.Drawing.Point(120, 32);
button1.Size =
new System.Drawing.Size(80, 24);
button1.TabIndex = 1;
button1.Text = "Merge Files";
button1.Click +=
new System.EventHandler(button1_Click);
textBox2.Location =
new System.Drawing.Point(144, 176);
textBox2.TabIndex = 5;
textBox2.Size =
new System.Drawing.Size(200, 20);
textBox2.Visible =
false;
textBox2.TextChanged +=
new System.EventHandler(textBox2_TextChanged);
button4.Location =
new System.Drawing.Point(144, 256);
button4.Size =
new System.Drawing.Size(75, 23);
button4.TabIndex = 7;
button4.Enabled =
false;
button4.Text = "Clear";
button4.Click +=
new System.EventHandler(button4_Click);
label1.Location =
new System.Drawing.Point(24, 40);
label1.Text = "Please select :";
label1.Size =
new System.Drawing.Size(80, 24);
label1.TabIndex = 0;
button3.Location =
new System.Drawing.Point(32, 256);
button3.Size =
new System.Drawing.Size(75, 23);
button3.TabIndex = 6;
button3.Enabled =
false;
button3.Text = "Proceed";
button3.Click +=
new System.EventHandler(button3_Click);
button2.Location =
new System.Drawing.Point(213, 32);
button2.Size =
new System.Drawing.Size(75, 23);
button2.TabIndex = 2;
button2.Text = "Split File";
button2.Click +=
new System.EventHandler(button2_Click);
this.Text = "Say Gin\'s SplitFile";
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(376, 309);
groupBox1.Controls.Add(button2);
groupBox1.Controls.Add(button1);
groupBox1.Controls.Add(label1);
this.Controls.Add(label4);
this.Controls.Add(button4);
this.Controls.Add(button3);
this.Controls.Add(textBox2);
this.Controls.Add(label3);
this.Controls.Add(textBox1);
this.Controls.Add(label2);
this.Controls.Add(groupBox1);
}
protected void textBox2_TextChanged(object sender, System.EventArgs e)
{
// Calculate the max size of each file
if(checkInput())
{
if(splitFile)
{
len = f.Length;
eachSize = (
int) Math.Ceil((double) len/x);
label4.Visible =
true;
label4.Text = "The max size of each file is approx "+eachSize+" kb";
}
button3.Enabled =
true;
}
}
protected void button4_Click(object sender, System.EventArgs e)
{
// Clear
resetAll();
label4.Visible =
false;
label4.Text = "";
}
protected void button3_Click(object sender, System.EventArgs e)
{
// Proceed
if(splitFile)
{
FileStream inFile =
new FileStream(f.ToString(),FileMode.OpenOrCreate, FileAccess.Read);
for (int i = 0; i<x ; i++)
{
FileStream outFile =
new FileStream(i+".sg",FileMode.OpenOrCreate, FileAccess.Write);
int data=0;
byte [] buffer = new byte [eachSize];
if ((data=inFile.Read(buffer,0,eachSize))>0)
{
outFile.Write(buffer,0,data);
}
outFile.Close();
}
inFile.Close();
label4.Visible =
true;
label4.Text = "SplitFile completed sucessfully";
}
else
{
FileStream outFile =
new FileStream(outputName,FileMode.OpenOrCreate, FileAccess.Write);
for (int i=0;i<x; i++)
{
int data=0;
byte [] buffer = new byte [1024];
FileStream inFile =
new FileStream(i+".sg",FileMode.OpenOrCreate, FileAccess.Read);
while ((data=inFile.Read(buffer,0,1024))>0)
{
outFile.Write(buffer,0,data);
}
inFile.Close();
}
outFile.Close();
label4.Visible =
true;
label4.Text = "MergeFile completed sucessfully";
}
}
protected void button2_Click(object sender, System.EventArgs e)
{
// Split
button1.Enabled = false;
button2.Enabled =
false;
button4.Enabled =
true;
label2.Visible =
true;
label2.Text = "Name of file to split :";
textBox1.Visible =
true;
textBox1.Text = "";
textBox1.Enabled =
true;
label3.Visible =
true;
label3.Text = "How many files to split into :";
textBox2.Visible =
true;
textBox2.Text = "";
splitFile =
true;
}
protected void button1_Click(object sender, System.EventArgs e)
{
// Merge
button1.Enabled = false;
button2.Enabled =
false;
button4.Enabled =
true;
label2.Visible =
true;
label2.Text = "Name of the first file :";
textBox1.Visible =
true;
textBox1.Text = "0.sg";
textBox1.Enabled =
false;
label3.Visible =
true;
label3.Text = "How many files to merge :";
textBox2.Visible =
true;
textBox2.Text = "";
splitFile =
false;
}
private bool checkInput()
{
if (textBox1.Text.Length ==0 || textBox2.Text.Length ==0)
{
label4.Visible =
true;
label4.Text = "The input(s) must not be blank";
return false;
}
try
{
x = Int32.FromString(textBox2.Text);
}
catch(Exception ex)
{
label4.Visible =
true;
label4.Text = "The second textfield must be a number";
Console.WriteLine("The exception => "+ex);
return false;
}
f =
new File(textBox1.Text);
if(!f.Exists)
{
label4.Visible =
true;
label4.Text = "The file "+textBox1.Text+" does not exist";
f =
null;
return false;
}
return true;
}
private void resetAll()
{
button1.Enabled =
true;
button2.Enabled =
true;
button3.Enabled =
false;
button4.Enabled =
false;
label2.Visible =
false;
label3.Visible =
false;
label2.Text = "";
label3.Text = "";
textBox1.Visible =
false;
textBox1.Text = "";
textBox2.Visible =
false;
textBox2.Text = "";
}
}
}





Article Extensions
Contents added by chad willson on Nov 28, 2009
Download File: FileSplit.zip

Hi all,

I added this code to a solution and cleaned it up a little. The code does work but it isn't the cleanest.

It is a good jumping off point.

 

COMMENT USING

Trending up