Click here to Skip to main content
15,867,330 members
Articles / Programming Languages / Visual Basic
Article

File Split & Merge Tool

Rate me:
Please Sign up or sign in to vote.
4.54/5 (18 votes)
9 Aug 20022 min read 207.2K   4.4K   46   17
This tool is used to split large files into smaller chunks and merge them back into a single file.

Sample Image - File_Split___Merge_Tool.jpg

Summary

This tool is used to split large files into smaller chunks of your choice and merge them back into single file. This is extremely useful when copying files into floppies or while transferring files over network.

Introduction

This project consists of a class SplitMerge.vb and a user interface to test the class. Source code is provided as a VS.NET project, so you can download the source and build it with current version of VS.NET. I created and tested this under VS.NET with SP2 under Windows 2000.

This project demonstrates the usage of file streams, threading and events in VB.NET.

Description

SplitMerge.VB

This class has two primary methods SplitFile and MergeFile.

  • SplitFile

    Splits files into smaller chunk files.

    • FileName: File name to split with full path
    • OutputPath: Output folder name where the chunk files will be created. Chunk files will be created with the same name as input file with suffix of sequence number (e.g.: bigfile.exe.001)
    • DeleteFileAfterSplit: Boolean value indicating, whether to delete the input file after splitting.
    • ChunkSize: Long value indicating the chunk size in bytes.
  • MergeFile

    Merges all the chunk files into one file.

    • FileName: First chunk file name with full path or the file name with full path (In this case c:\bigfile.exe.001 or c:\bigfile.exe)
    • OutputPath: Output folder name where the merged file will be created
    • DeleteFilesAfterMerge: Boolean value indicating, whether to delete the chunk file after merge.

Both these methods are thread safe and can be called as background threads

Events

This class has 3 events.

  • FileSplitCompleted  - This event is raised after the split process is completed
  • FileMergeCompleted - This event is raised after the merge process is completed
  • UpdateProgress - This event is raised to notify the client about the progress. Raised after each chunk file is created.

Using SplitMerge class

Add a form to your project. Declare SplitMerge class with events.

VB
Private WithEvents _FileSplitMerge As New SplitMerge() 

Declare a thread to call the Split and Merge Methods

VB
Private backgroundThread As System.Threading.Thread

See the demo for other controls 

Calling Split process

VB
Private Sub SplitFile()
    With _FileSplitMerge.ChunkSize = txtChunkSize.Text
        .FileName = txtFileName.Text
        .OutputPath = txtOutputFolder.Text
        .DeleteFileAfterSplit = chkOption.Checked
        backgroundThread = New _ 
         Threading.Thread(AddressOf .SplitFile)backgroundThread.Start()
    End With
End Sub

After the split process is completed, it will raise the FileSplitCompleted event.

FileSplitCompleted event will return an ErrorMessage, in case of an error it will return the error message else an empty string.

VB
Private Sub _FileSplitMerge_FileSplitCompleted(ByVal _ 
          ErrorMessage As String) Handles _FileSplitMerge.FileSplitCompleted
    If ErrorMessage = "" Then
        MsgBox("File Split process completed")
    Else
        MsgBox("File Split process Failed" & NEWLINE & ErrorMessage)
    End If
    ResetControls()
End Sub

So check for the ErrorMessage to display appropriate message.

Calling Merge procedure

VB
Private Sub MergeFiles()
    With _FileSplitMerge.FileName = txtFileName.Text
        .OutputPath = txtOutputFolder.Text
        .DeleteFilesAfterMerge = chkOption.Checked
        backgroundThread = New _ 
          Threading.Thread(AddressOf .MergeFile)backgroundThread.Start()
    End With
End Sub

Events are the same as Split process.

Demo comes with a fully functional example.

Notes & ToDo features

I would like to enhance this further by adding Crypt, Compression and password functionalities in next version.

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


Written By
Web Developer
United States United States
Working as consultant in Client/server,Web Development, Automation areas using C#,Vb,Vb.net,Asp.net,Sql Server,Xml,Html&Dhtml etc..
Looking for a challenging project in .NET

Comments and Discussions

 
QuestionProblem with large files Pin
Muthuraj81422-Dec-11 20:39
Muthuraj81422-Dec-11 20:39 
QuestionSpliting files with different extensions in vb.net Pin
Pavi345612-Jul-11 3:42
Pavi345612-Jul-11 3:42 
GeneralGreat! Pin
Jesse Garcia28-Jul-08 20:43
Jesse Garcia28-Jul-08 20:43 
GeneralI want to know.... [modified] Pin
tj_istar9-Aug-07 2:42
tj_istar9-Aug-07 2:42 
GeneralMessage Closed Pin
27-Jun-07 18:14
winnovative27-Jun-07 18:14 
GeneralRe: Commercial Split/Merge Pin
jzonthemtn23-Feb-08 7:53
jzonthemtn23-Feb-08 7:53 
QuestionHow do i send the split files over LAN? Pin
redkops30-Aug-05 7:56
redkops30-Aug-05 7:56 
GeneralDoesn't work for large files Pin
luke73489728924-Aug-05 9:09
luke73489728924-Aug-05 9:09 
GeneralRe: Doesn't work for large files Pin
luke73489728924-Aug-05 9:26
luke73489728924-Aug-05 9:26 
Might want to consider using the BufferedStream class so that the whole file isn't read into memory at once. See C# example at http://www.codeproject.com/csharp/filefix.asp
Generalslow but good Pin
Ahmed ceder25-Sep-04 11:17
Ahmed ceder25-Sep-04 11:17 
GeneralRe: slow but good Pin
eRRaTuM30-Mar-06 4:21
eRRaTuM30-Mar-06 4:21 
GeneralPDF File Merge Pin
Zulfikar Ali4-Oct-02 9:37
Zulfikar Ali4-Oct-02 9:37 
GeneralRe: PDF File Merge Pin
Sreenivas Vemulapalli4-Oct-02 11:26
Sreenivas Vemulapalli4-Oct-02 11:26 
Generaltry this Pin
Sreenivas Vemulapalli4-Oct-02 11:29
Sreenivas Vemulapalli4-Oct-02 11:29 
GeneralRe: try this Pin
Zulfikar Ali4-Oct-02 11:50
Zulfikar Ali4-Oct-02 11:50 
GeneralSpeed enhancement Pin
Paul A. Howes12-Aug-02 2:47
Paul A. Howes12-Aug-02 2:47 
GeneralRe: Speed enhancement Pin
Sreenivas Vemulapalli12-Aug-02 12:26
Sreenivas Vemulapalli12-Aug-02 12:26 

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.