Click here to Skip to main content
Licence GPL3
First Posted 30 Mar 2006
Views 26,587
Downloads 243
Bookmarked 46 times

The FileSplitter reLoaded

By | 11 Aug 2008 | Article
Utilty to quickly split and merge files.

Sample Image - TheFileSplitterv2Processing.jpg

Introduction

Everybody needs at least twice a file splitter utility. One day I created my own just to do the things to be done :), and then I realized that I could make it run much faster (the FileSPlitter v0.1 runs @ 1~2bytes/hour ^_^), so I inserted some multithreading, some event handling, enlarged the cache used, and of course, rewrote the split and merge methods. Now, it's quite fast: up to 20 Mb/s.

Note

The file splitter is now in a separate class, exposing different commented constructors, methods, and properties. It is disposable and gives the speed of the processing.

Using the code

Just instantiate the FileSplitter, manage its events, and begin processing.

FileSplitter.Splitter fs = new FileSplitter.Splitter(dlgOpen.FileName, 
                           dlgSaveTo.SelectedPath, CacheSize, SizeLimit);

fs.SplitDone += new EventHandler(fs_SplitDone);
fs.partialSplitDone += new EventHandler(fs_partialSplitDone);
fs.SplitError += new EventHandler(fs_SplitError);
fs.BeginSplit();

The Split method is private to the FileSplitter class:

private void Split()
{
    if (m_CacheSize > m_SizeLimit)
        m_CacheSize = (uint)m_SizeLimit;

    byte[] cBuffer = new byte[m_CacheSize];
    int nCounter = 0;

    m_fsIn = new FileStream(m_FileName, FileMode.Open, FileAccess.Read);
    m_bReader = new BinaryReader(m_fsIn);


    if (!Directory.Exists(m_OutDir))
    Directory.CreateDirectory(m_OutDir);
    else
    {
        Directory.Delete(m_OutDir, true);
            Directory.CreateDirectory(m_OutDir);
    }

    int reads = 0;
    try
    {
        do
        {
            m_fsOut = new FileStream(m_OutDir + "\\" + 
                      nCounter.ToString() + ".part", FileMode.Create);
            do
            {
                if ((m_fsIn.Length - m_fsIn.Position) < cBuffer.Length)
                    cBuffer = new byte[m_fsIn.Length - m_fsIn.Position];
                reads = m_bReader.Read(cBuffer, 0, cBuffer.Length);
                m_bWriter = new BinaryWriter(m_fsOut);
                m_bWriter.Write(cBuffer, 0, reads);
                m_Written += reads;// = fsIn.Position;
                m_Progress = (uint)((float)m_Written * 100 / (float)m_FileSize);
                OnPartialSplitDone(EventArgs.Empty);
            } while ((m_fsOut.Position < m_SizeLimit) && 
                     (m_fsIn.Position < m_FileSize));
            m_bWriter.BaseStream.Close();
            m_Written = m_fsIn.Position;
            nCounter++;
            m_Progress = (uint)((float)m_Written * 100 / (float)m_FileSize);
            OnPartialSplitDone(EventArgs.Empty);
        } while ((m_fsIn.Position < m_fsIn.Length));
        m_bReader.BaseStream.Close();
        OnSplitDone(EventArgs.Empty);
    }
    catch (Exception e)
    {
        m_SplitErrorMessage = e.Message;
        OnError(EventArgs.Empty);
        abort();
    }
    GC.Collect();
}
.
.
.

public void BeginMerge()
{
    m_tdMerger = new Thread(new ThreadStart(merge));
    m_tdMerger.Priority = ThreadPriority.AboveNormal;
    m_tdMerger.IsBackground = false;
    m_tdMerger.Name = "Merging";
    m_TimeStart = DateTime.Now;
    m_tdMerger.Start();
}

Methods

  • public FileSplitter(string FileName, string DestinationFolder, int CacheSize, int SizeLimit): Fully qualified constructor - Splitting.
  • public FileSplitter(string FileName, string DestinationFolder, int SizeLimit): Constructor with default cache - Splitting.
  • public FileSplitter(string SourceFolder, int CacheSize): Fully qualified constructor - Merging.
  • public FileSplitter(string SourceFolder): Constructor with default cache - Merging.
  • public void Dispose(): Disposes the class instance.
  • public void BeginMerge(): Begins the merging thread.
  • public void BeginSplit(): Begins the splitting thread.

Events

  • public event EventHandler partialCopyDone: Triggered when a partial processing is done.
  • public event EventHandler CopyDone;: Triggered when processing is done.
  • public event EventHandler Error;: Triggered when an error occurs.

Properties

  • public string ErrorMessage: Last error message
  • public int Progress: Percentage of the entire progress (nice to use with a progress bar).
  • public long TotalDone: Total size processed.

History

  • v2.1: Final version, with real classes, events, and multithreading.
  • V2.0: Events, multithreading, nicer and smoother.
  • v0.1: The beginning.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

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
QuestionGreat little dll file Pinmemberrspercy6012:31 2 Oct '11  
NewsNew VERSION PinmembereRRaTuM1:26 11 Aug '08  
GeneralFeedback/bugs PinmemberSander van Driel0:30 4 Apr '06  
GeneralRe: Feedback/bugs PinmemberMikePEQ10:44 5 Apr '06  
AnswerRe: Feedback/bugs PinmembereRRaTuM2:20 10 Apr '06  
AnswerRe: Feedback/bugs PinmembereRRaTuM1:36 10 Apr '06  
GeneralRe: Feedback/bugs PinmemberSander van Driel2:23 10 Apr '06  
GeneralRe: Feedback/bugs PinmembereRRaTuM1:46 28 Jun '06  

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 11 Aug 2008
Article Copyright 2006 by eRRaTuM
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid