Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Classes for Splitting and Joining files

0.00/5 (No votes)
16 Aug 2002 1  
This article describes two classes, one for splitting a file into smaller chunks and another for merging smaller files into one. Also there is a utility written using these classes.

Introduction

This article explains two classes CFileSplit and CFileMerge.

CFileSplit

This class is used for splitting a file into multiple parts. There are two ways to split a file using this :- split into a fixed number of files, or split into files of a specific size each. The class is derived from CWinThread class. Implementing this as a thread means that -

  1. Any application using this class won't freeze while it is processing
  2. You can cancel a lengthy split process. Also the thread will send messages back to the calling window indicating the progress of the split or merge process.

Using CFileSplit is pretty simple. Include the FileSplit.h and FileSplit.cpp files in your project. Create an object of CFileSplit using the AfxBeginThread function and set the input file name, output directory, and how to split ( i.e. fixed no: of files or fixed size files). Also set the m_pMainWnd member of the CWinThread object to the current window. This will enable it to send messages back to it.

CFileSplit *m_pFS;
	
.
.
.

m_pFS = (CFileSplit*)(AfxBeginThread(RUNTIME_CLASS(CFileSplit)));
m_pFS->m_pMainWnd = this;
m_pFS->m_InputFileName = m_InputFile;
m_pFS->m_OutputDirName = m_OutputDir;
m_pFS->m_nSplitMode    = 0;
m_pFS->m_nSplitVal     = 12;

m_nSplitMode member indicates how to split. 0 means split into a fixed no: of files, in which case the no: of files will be in m_nSplitVal. If m_nSplitMode is 1 it means split into fixed size files, in which case m_nSplitVal will contain the size of each output file in Bytes.

To start the splitting process send a WM_FS_START message to the CFileSplit object. WM_FS_START is an application defined message defined in FileSplit.h

m_pFS->PostThreadMessage(WM_FS_START,0,0);

The thread will destroy itself after it is done splitting the files.

If you want to track the progress of a splitting process, the CFileSplit thread will send you an WM_FS_UPDATESTATUS message. The WPARAM of this message will contain the progress in percentage. The LPARAM will be set to 1 when the splitting is complete. You can handle this message in your application; in your message map, add this :-

BEGIN_MESSAGE_MAP(CFileSplitPage, CPropertyPage)
    //{{AFX_MSG_MAP(CFileSplitPage)

    .
    .
    .
    //}}AFX_MSG_MAP

    ON_MESSAGE(WM_FS_UPDATESTATUS, OnUpdateStatus)
END_MESSAGE_MAP()

where OnUpdateStatus is the handler function which is declared as:

afx_msg void OnUpdateStatus(WPARAM wP, LPARAM lP);

CFileMerge

This class does the opposite of the above; it takes a bunch of files and combines it into one. (copy /b command in DOS can also do the same thing). It is implemented similar to CFileSplit and can be used as follows:

CFileMerge *m_pFM;
.
.
.
m_pFM = (CFileMerge*)AfxBeginThread(RUNTIME_CLASS(CFileMerge));
m_pFM->m_pMainWnd=this;
m_pFM->m_InFiles.Copy(arr);

The m_InFiles member is a CStringArray containing the list of input files to be combined, in the order in which they are to be combined.

m_pFM->m_OutFile = m_OutFile;
m_pFM->PostThreadMessage(WM_FM_START,0,0);

You send a WM_FM_START message to start the process. Similar to CFileSpit, you get a WM_FM_UPDATESTATUS message indicating the progress of the merge.

The Sample Application

The sample app provided is written using these above classes. It is a dialog based application but I changed the main Dialog class to derive from CPropertySheet instead of CDialog and added split and merge as two pages to it. (As you can see I was not able to get rid of the 'help' button from the Property sheet, nor add a minimize button).

From the Split Page, you can select the file you want to split, select the output directory and hit the start button. The application creates Files with numbered extensions ( outfile.ext.1, outfile.ext.2 and so on...)

From the merge page select the input files. When you add files, it tries to sort based on the extensions, if they are numbered. If they are not in the right order, you can move them up and down. Hit the start button to start merging. During a long split or merge process, you can hit the cancel button to stop the process.

History

17 Aug 2002 - updated downloads

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here