Click here to Skip to main content
Licence CPOL
First Posted 25 Nov 2004
Views 61,616
Bookmarked 30 times

Utilty to split and merge files

By | 6 Dec 2004 | Article
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.

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)

About the Author

eRRaTuM

Architect

Morocco Morocco

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 2 PinmemberMidax10:28 13 Apr '11  
GeneralThe sizes of files do not coincide Pinmemberserg1310:03 16 Jun '10  
Generalasync app PinmemberSteveKuznicki11:18 1 May '09  
NewsNew VERSION PinmembereRRaTuM1:29 11 Aug '08  
Generalhi PinmemberRam Nataraan3:17 1 Aug '08  
GeneralRe: hi PinmembereRRaTuM3:09 5 Aug '08  
NewsRe: hi PinmembereRRaTuM1:29 11 Aug '08  
GeneralYou should rename the title of your article to Mp3 splitter or generic splitter Pinmemberrj4510:30 26 Jan '08  
GeneralRe: You should rename the title of your article to Mp3 splitter or generic splitter PinmembereRRaTuM0:51 11 Feb '08  
QuestionWhere's the link to download the code? PinmemberJCrane22:24 7 Dec '04  
AnswerRe: Where's the link to download the code? PinmembereRRaTuM7:13 7 Dec '04  
AnswerRe: Where's the link to download the code? PinmembereRRaTuM9:31 8 Dec '04  
GeneralAnother Version... PinmemberShawn Cicoria7:57 6 Dec '04  
GeneralRe: Another Version... PinmembereRRaTuM9:41 8 Dec '04  
Generalnew byte[] Pinmemberynik8:03 27 Nov '04  
GeneralRe: new byte[] PinmembereRRaTuM12:57 1 Dec '04  
GeneralRe: new byte[] Pinmemberynik3:12 2 Dec '04  
GeneralRe: new byte[] PinmembereRRaTuM10:44 2 Dec '04  
GeneralMaking it Asynchronous Pinmemberromias1:21 26 Nov '04  
GeneralRe: Making it Asynchronous PinmembereRRaTuM12:58 1 Dec '04  
GeneralRe: Making it Asynchronous PinmembereRRaTuM9:53 8 Dec '04  
GeneralRe: Making it Asynchronous PinmembereRRaTuM14:42 15 Jan '05  
QuestionFile upload?!? Pinmemberhorsted13:49 25 Nov '04  
AnswerRe: File upload?!? PinmemberBrian Delahunty22:07 25 Nov '04  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 6 Dec 2004
Article Copyright 2004 by eRRaTuM
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid