65.9K
CodeProject is changing. Read more.
Home

File Split & Merge Tool

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.54/5 (16 votes)

Aug 10, 2002

2 min read

viewsIcon

209373

downloadIcon

4457

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.

Private WithEvents _FileSplitMerge As New SplitMerge() 

Declare a thread to call the Split and Merge Methods

Private backgroundThread As System.Threading.Thread

See the demo for other controls 

Calling Split process

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.

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

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.