Click here to Skip to main content
15,902,938 members
Articles / Desktop Programming / Windows Forms
Article

Utilty to split and merge files

Rate me:
Please Sign up or sign in to vote.
2.71/5 (16 votes)
6 Dec 2004CPOL2 min read 93.6K   2.1K   30   24
A simple C#/.NET utility to split files (upto 2 GB) to parts.

Sample image

Introduction

Well, I was working simultaneously on a USB-less NIC-less desktop and on a CD-less laptop, and I needed the FXCop utility, which was in the desktop, for the laptop. The only possible way to do so was to split it in the desktop, so as to copy its parts to the laptop (none of them is connected to internet), and then merge them back. I went back some years earlier, when I was fond of Borland C and MS DOS, and I wrote a utility in C. I first tried it on a MP3 file in order to test if it works really, then passed to a packaged installer (because it checks integrity of the file), and finally I decided to rewrite it in C#...

Note

The FileSplitter just reads sequentially blocks of 8 Kb or 16 Kb from the file to split, and writes it into the first part file "0.part" in a directory named the same name as the name of the file to split, created in the directory selected using "Browse" button. It writes the first part until it reaches the desired end, then it creates another one "1.part" and so on until it reaches the end of the file to split. For merging, the FileSplitter reads every "part" file in the selected directory and merges them incrementally. It is now done asynchronously using threads. The user can cancel any process (the File/Directory in progress of creation is deleted).

Using the code

Actually, merging was much easier to develop than splitting... I used two file streams the first to point to the file to split and the other to the file The FileSplitter uses a BinayReader and a BinaryWriter to optimize reading and writing a little bit. All the jobs are stored in two methods in one class: the form class.

C#
private bool Splitter(string strFileName,string strPathName,long lgSize)
{
    ...
    prgbProgress.Step = (int) System.Math.Ceiling( ( (float)FileSize / 10 ));

    //split it to parts in a folder Called "FileName"
    System.IO.Directory.CreateDirectory(strDirectory);

    //begin writing
    while ( FSIn.Position != FSIn.Length )
    {
        PreDefinedCacheSize = 8192;
        byte [] buffer = new byte [PreDefinedCacheSize];
        strNewFileNames = strDirectory + "//" + intCounter.ToString() + 
                          ".part" + intCounter.ToString() + ".part";
        FSout = new FileStream(strNewFileNames,FileMode.Create);
        BinaryWriter wFSOut = new BinaryWriter(FSout);
        while ((FSout.Position  < lgSize) && (FSIn.Position != FSIn.Length ))
        {
            prgbProgress.Value = (int) (( (float) FSIn.Position / 
                  (float) FSIn.Length ) * (float) prgbProgress.Maximum);
            if ( ((FSIn.Length - FSIn.Position) < Math.Min(PreDefinedCacheSize, 
                              (int)lgSize)) && (PreDefinedCacheSize > lgSize) )
            {
                PreDefinedCacheSize = (int)FSIn.Length - (int)FSIn.Position;
                rFSIn.Read(buffer,0,PreDefinedCacheSize);
                wFSOut.Write(buffer);
                Thread.Sleep(1);
            }
            else
            {
                if ( PreDefinedCacheSize > lgSize ) PreDefinedCacheSize = (int)lgSize;
                rFSIn.Read(buffer,0,PreDefinedCacheSize);
                wFSOut.Write(buffer);
                Thread.Sleep(1);
            }
        }
        wFSOut.Close();
        FSout.Close();
        intCounter++;
    }
    //finish
    MessageBox.Show("Done!! splitting "+strFileName,"Splitter");
    CleanUp();
    rFSIn.Close();
    return true;
}

void TheSplitter() //the splitter method to be invoked
{
    int SizeLimit = 0;
    if (rdbFree.Checked)
    {
        if (txtSize.TextLength != 0 )SizeLimit = Int32.Parse(txtSize.Text);
        if (rdbKiloBytes.Checked) SizeLimit *= 1024;
        if (rdbMegaBytes.Checked) SizeLimit *= 1024*1024;
    }
    else if (rdbCD.Checked) SizeLimit = CDSize;
    else SizeLimit = FDDSize;
    Splitter(txtFileName.Text,txtFolderPath.Text,SizeLimit);
}
  • FSIn: File stream for the file to split.
  • FSout: File stream for parts.
  • PreDefinedCacheSize: Block size to read and write.
  • void CleanUp(): closes streams, restores the form appearance if a wrong entry was submitted.
  • string FileName(string): returns the file name from a path.
  • bool Merger(string): merges the files in the path given in the string.
  • bool Splitter(string1, string2, long_): Splits the file given in string1 into folder string2 in long_ chunks.
  • void TheSplitter(): Method to invoke the splitter.
  • void TheMerger(): Method to invoke the merger.

History

Some one once said that History is What U made some milliseconds earlier... and so history of human beings was built.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer
Morocco Morocco
in his studies, erratum discovered c/c++.he appreciated it.
when he met oracle products, in his job, he fell in love.
he uses c# .net & ms sql.

he created a "f.r.i.e.n.d.s" like soap movie, melting all of the above.
went back in the university.
after he took courses of artificial vision & imagery, he finished his studies with a successful license plate recognition project.

Comments and Discussions

 
GeneralMy vote of 2 Pin
Midax13-Apr-11 10:28
Midax13-Apr-11 10:28 
GeneralThe sizes of files do not coincide Pin
serg1316-Jun-10 10:03
serg1316-Jun-10 10:03 
Generalasync app Pin
SteveKuznicki1-May-09 11:18
SteveKuznicki1-May-09 11:18 
NewsNew VERSION Pin
eRRaTuM11-Aug-08 1:29
eRRaTuM11-Aug-08 1:29 
Generalhi Pin
Ram Nataraan1-Aug-08 3:17
Ram Nataraan1-Aug-08 3:17 
GeneralRe: hi Pin
eRRaTuM5-Aug-08 3:09
eRRaTuM5-Aug-08 3:09 
NewsRe: hi Pin
eRRaTuM11-Aug-08 1:29
eRRaTuM11-Aug-08 1:29 
GeneralYou should rename the title of your article to Mp3 splitter or generic splitter Pin
rj4526-Jan-08 10:30
rj4526-Jan-08 10:30 
Rar splits files perfectly and its free so why would u bother to split something and not compress it? i donno..

I was searching for a mp3 splitter and there are a ton of shity ones out there that are not free... this is perfect for those long house mixes which u really want as seperate tracks.

Beautiful, good job mate..!
GeneralRe: You should rename the title of your article to Mp3 splitter or generic splitter Pin
eRRaTuM11-Feb-08 0:51
eRRaTuM11-Feb-08 0:51 
QuestionWhere's the link to download the code? Pin
JCrane27-Dec-04 2:24
JCrane27-Dec-04 2:24 
AnswerRe: Where's the link to download the code? Pin
eRRaTuM7-Dec-04 7:13
eRRaTuM7-Dec-04 7:13 
AnswerRe: Where's the link to download the code? Pin
eRRaTuM8-Dec-04 9:31
eRRaTuM8-Dec-04 9:31 
GeneralAnother Version... Pin
Shawn Cicoria6-Dec-04 7:57
Shawn Cicoria6-Dec-04 7:57 
GeneralRe: Another Version... Pin
eRRaTuM8-Dec-04 9:41
eRRaTuM8-Dec-04 9:41 
Generalnew byte[] Pin
Daniel Grunwald27-Nov-04 8:03
Daniel Grunwald27-Nov-04 8:03 
GeneralRe: new byte[] Pin
eRRaTuM1-Dec-04 12:57
eRRaTuM1-Dec-04 12:57 
GeneralRe: new byte[] Pin
Daniel Grunwald2-Dec-04 3:12
Daniel Grunwald2-Dec-04 3:12 
GeneralRe: new byte[] Pin
eRRaTuM2-Dec-04 10:44
eRRaTuM2-Dec-04 10:44 
GeneralMaking it Asynchronous Pin
romias26-Nov-04 1:21
romias26-Nov-04 1:21 
GeneralRe: Making it Asynchronous Pin
eRRaTuM1-Dec-04 12:58
eRRaTuM1-Dec-04 12:58 
GeneralRe: Making it Asynchronous Pin
eRRaTuM8-Dec-04 9:53
eRRaTuM8-Dec-04 9:53 
GeneralRe: Making it Asynchronous Pin
eRRaTuM15-Jan-05 14:42
eRRaTuM15-Jan-05 14:42 
QuestionFile upload?!? Pin
horsted25-Nov-04 13:49
horsted25-Nov-04 13:49 
AnswerRe: File upload?!? Pin
Brian Delahunty25-Nov-04 22:07
Brian Delahunty25-Nov-04 22:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.