File splitting code

Source: Internet
Author: User

Imports System. IO
''''
''Developed by: ivivas Vemulapalli vemvas@yahoo.com
''Date: 08/10/2002
''Version: 1.0.0.0
''Description: To split the file into smaller chunks and merge them back to single file
''Ororts Multi threading/Thread safe
''Remarks: Can be used to copy big file into floppies, copying file over network/internet
''
''Future enhancements: Add Crypt and compression functionality
Public Class SplitMerge
'''
Private _ FileName As String
Private _ ChunkSize As Integer
Private _ Error As String
Private _ OutPutPath As String
Private _ DeleteFileAfterSplit As Boolean
Private _ DeleteFilesAfterMerge As Boolean
Public Event FileSplitCompleted (ByVal ErrorMessage As String)
Public Event FileMergeCompleted (ByVal ErrorMessage As String)
Public Event UpdateProgress (ByVal Progress As Double)
'''
Public Property DeleteFilesAfterMerge () As Boolean
Get
Return _ DeleteFilesAfterMerge
End Get
Set (ByVal Value As Boolean)
_ DeleteFilesAfterMerge = Value
End Set
End Property
'''
Public Property DeleteFileAfterSplit () As Boolean
Get
Return _ DeleteFileAfterSplit
End Get
Set (ByVal Value As Boolean)
_ DeleteFileAfterSplit = Value
End Set
End Property
'''
Public Property OutputPath () As String
Get
Return _ OutPutPath
End Get
Set (ByVal Value As String)
If Value. Length> 0 AndAlso Value. Substring (Value. Length-1, 1) = "\" Then Value = Value. Substring (0, Value. Length-1)
_ OutPutPath = Value
End Set
End Property
'''
Public ReadOnly Property getError () As String
Get
Return _ Error
End Get
End Property
'''
Public Sub ClearErrors ()
_ Error = ""
End Sub
'''
Public Property FileName () As String
Get
Return _ FileName
End Get
Set (ByVal Value As String)
If Value <> "" Then
If Value. Substring (Value. Length-4, 4) = ". 001" Then
_ FileName = Value. Substring (0, Value. Length-4)
Else
_ FileName = Value
End If
End If
End Set
End Property
'''
Public Property ChunkSize () As Integer
Get
Return _ ChunkSize
End Get
Set (ByVal Value As Integer)
_ ChunkSize = Value
End Set
End Property
'''
Public Sub SplitFile ()
'''
''Usage
''Fille FileName and ChunkSize
''If output Folder is different than the input file then pass it, otherwise chunk files will be created in
''Same folder.
''Chunk File Size shocould be less than the input file size.
''Client application is informed via the FileSplitCompleted event
''Ss SS can be updated via UpdateProgress event
Dim _ FileSize As Long
Dim _ Index As Short
Dim _ OutputFile As String
Dim _ BaseName As String
Dim _ StartPosition As Long
Dim _ Buffer As Byte () = New Byte (){}
Dim _ InputFileStram As System. IO. FileStream
Dim _ OutputFileStram As System. IO. FileStream
Dim _ BinaryWriter As BinaryWriter
Dim _ BinaryReader As BinaryReader
Dim _ Fragments As Short
Dim _ RemainingBytes As Long
Dim _ SplitFileName As String
Dim _ Progress As Double
'''
Try
_ Error = ""
_ SplitFileName = Me. FileName
_ InputFileStram = New FileStream (FileName, FileMode. Open)
_ BinaryReader = New BinaryReader (_ InputFileStram)
_ FileSize = FileSize (_ SplitFileName)
_ BaseName = FileBaseName (_ SplitFileName)
If Not File. Exists (_ SplitFileName) Then _ Error = "File" & FileName & "doesn't exist"
If _ FileSize <= ChunkSize Then _ Error = _ SplitFileName & "size (" & _ FileSize & ") is less than the ChunkSize (" & ChunkSize &")"
If _ Error = "" Then
_ Fragments = Math. Floor (_ FileSize/ChunkSize)
_ Progress = 100/_ Fragments
_ RemainingBytes = _ FileSize-(_ Fragments * ChunkSize)
If Me. OutputPath = "" Then Me. OutputPath = Directory. GetParent (_ SplitFileName). ToString
If Not Directory. Exists (Me. OutputPath) Then Directory. CreateDirectory (Me. OutputPath)
_ BinaryReader. BaseStream. Seek (0, SeekOrigin. Begin)
For _ Index = 1 To _ Fragments
_ OutputFile = Me. OutputPath & "\" & _ BaseName & "." & Format (_ Index, "000 ")
ReDim _ Buffer (ChunkSize-1)
_ BinaryReader. Read (_ Buffer, 0, ChunkSize)
_ StartPosition = _ BinaryReader. BaseStream. Seek (0, SeekOrigin. Current)
If File. Exists (_ OutputFile) Then File. Delete (_ OutputFile)
_ OutputFileStram = New System. IO. FileStream (_ OutputFile, FileMode. Create)
_ BinaryWriter = New BinaryWriter (_ OutputFileStram)
_ BinaryWriter. Write (_ Buffer)
_ OutputFileStram. Flush ()
_ BinaryWriter. Close ()
_ OutputFileStram. Close ()
RaiseEvent UpdateProgress (_ Progress)
Next
If _ RemainingBytes> 0 Then
_ OutputFile = Me. OutputPath & "\" & _ BaseName & "." & Format (_ Index, "000 ")
ReDim _ Buffer (_ RemainingBytes-1)
_ BinaryReader. Read (_ Buffer, 0, _ RemainingBytes)
If File. Exists (_ OutputFile) Then File. Delete (_ OutputFile)
_ OutputFileStram = New System. IO. FileStream (_ OutputFile, FileMode. Create)
_ BinaryWriter = New BinaryWriter (_ OutputFileStram)
_ BinaryWriter. Write (_ Buffer)
_ OutputFileStram. Flush ()
_ BinaryWriter. Close ()
_ OutputFileStram. Close ()
End If
_ InputFileStram. Close ()
_ BinaryReader. Close ()
If Me. DeleteFileAfterSplit Then File. Delete (FileName)
Me. _ Error = ""
End If
Catch ex As Exception
Me. _ Error = ex. ToString
Finally
RaiseEvent UpdateProgress (100)
RaiseEvent FileSplitCompleted (Me. _ Error)
_ BinaryWriter = Nothing
_ OutputFileStram = Nothing
_ BinaryReader = Nothing
_ InputFileStram = Nothing
End Try
End Sub
'''
Public Sub MergeFile ()
'''
''Usage
''Fille FileName and ChunkSize
''If output Folder is different than the input file then pass it, otherwise the merged file will be created in
''Same folder.
''Client application is informed via the FileMergeCompleted event
''Ss SS can be updated via UpdateProgress event

Dim _ InputFileStram As System. IO. FileStream
Dim _ OutputFileStram As System. IO. FileStream
Dim _ BinaryWriter As BinaryWriter
Dim _ BinaryReader As BinaryReader
Dim _ MergeFiles () As String
Dim _ index As Short
Dim _ buffer () As Byte = New Byte (){}
Dim _ FileSize As Long
Dim _ MergedFile As String
Dim _ MergingFileName As String
Dim _ Progress As Double
Try
_ Error = ""
_ MergingFileName = Me. FileName
If Me. FileName = "" Then _ Error = "Merging File Name can't be empty"
If _ Error = "" Then
_ MergeFiles = getMergeFiles (_ MergingFileName)
_ Progress = 100/(_ MergeFiles. Length-1)
If Not IsNothing (_ MergeFiles) Then
If Me. OutputPath = "" Then Me. OutputPath = Directory. GetParent (_ MergingFileName). ToString
If Not Directory. Exists (Me. OutputPath) Then Directory. CreateDirectory (Me. OutputPath)
_ MergedFile = Me. OutputPath & "\" & FileBaseName (_ MergingFileName)
If File. Exists (_ MergedFile) Then File. Delete (_ MergedFile)
_ OutputFileStram = New FileStream (_ MergedFile, FileMode. CreateNew)
_ BinaryWriter = New BinaryWriter (_ OutputFileStram)
For _ index = 0 To _ MergeFiles. Length-1
_ FileSize = FileSize (_ MergeFiles (_ index ))
_ InputFileStram = New FileStream (_ MergeFiles (_ index), FileMode. Open)
_ BinaryReader = New BinaryReader (_ InputFileStram)
_ BinaryReader. BaseStream. Seek (0, SeekOrigin. Begin)
ReDim _ buffer (_ FileSize-1)
_ BinaryReader. Read (_ buffer, 0, _ buffer. Length)
_ BinaryWriter. Write (_ buffer)
_ OutputFileStram. Flush ()
_ BinaryReader. Close ()
_ InputFileStram. Close ()
RaiseEvent UpdateProgress (_ Progress)
Next
_ OutputFileStram. Close ()
_ BinaryWriter. Close ()
If Me. DeleteFilesAfterMerge Then
For _ index = 0 To _ MergeFiles. Length-1
File. Delete (_ MergeFiles (_ index ))
Next
End If
Else
_ Error = "There are not merging files found in" & Me. OutputPath
End If
End If
Catch ex As Exception
Me. _ Error = ex. ToString
Finally
RaiseEvent UpdateProgress (100)
RaiseEvent FileMergeCompleted (Me. _ Error)
_ OutputFileStram = Nothing
_ BinaryWriter = Nothing
_ BinaryReader = Nothing
_ InputFileStram = Nothing
End Try
End Sub
'''
Public Function FileSize (ByVal FileName As String) As Long
Dim _ fileInfo As System. IO. FileInfo
_ FileInfo = New FileInfo (FileName)
Return _ fileInfo. Length
_ FileInfo = Nothing
End Function
'''
Private Function FileBaseName (ByVal FileName As String) As String
Dim _ fileInfo As System. IO. FileInfo
_ FileInfo = New FileInfo (FileName)
Return _ fileInfo. Name
_ FileInfo = Nothing
End Function
'''
Private Function getMergeFiles (ByVal MergeFileName As String) As String ()
Dim _ tempFiles As String () = System. IO. Directory. GetFiles (System. IO. Directory. GetParent (MergeFileName). ToString)
Dim _ MergeFiles () As String
Dim _ Index As Short
Do
_ Index + = 1
If Array. BinarySearch (_ tempFiles, MergeFileName & "." & Format (_ Index, "000")> = 0 Then
ReDim Preserve _ MergeFiles (_ Index-1)
_ MergeFiles (_ Index-1) = MergeFileName & "." & Format (_ Index, "000 ")
Else
Exit Do
End If
Loop
Return _ MergeFiles
End Function
'''
End Class
'''

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.