Advertisement
master131

VB.NET File Splitting/Joining

Apr 14th, 2011
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. '==========================
  2. 'Simple Splitting/Joining code for VB.NET
  3. 'Created and coded by master131
  4. '==========================
  5.  
  6. 'Usage:
  7. 'SplitFile("C:\test.exe", 2)
  8. 'JoinFile("C:\test.exe.001")
  9.  
  10. Private Sub JoinFile(ByVal path As String)
  11.     If Not IO.File.Exists(path) Then
  12.         Throw New ArgumentException("The file '" & path & "' does not exist!")
  13.         Exit Sub
  14.     End If
  15.  
  16.     If Not path.EndsWith(".001") Then
  17.         Throw New ArgumentException("'" & path & "' is not a valid .001 file!")
  18.         Exit Sub
  19.     End If
  20.  
  21.     Dim joinList As New List(Of String)
  22.     Dim joinIndex As Integer = 1
  23.     Dim joinName As String = IO.Path.Combine(IO.Path.GetDirectoryName(path), IO.Path.GetFileNameWithoutExtension(path) & "." & joinIndex.ToString("D3"))
  24.     While IO.File.Exists(joinName)
  25.         joinName = IO.Path.Combine(IO.Path.GetDirectoryName(path), IO.Path.GetFileNameWithoutExtension(path) & "." & joinIndex.ToString("D3"))
  26.         If IO.File.Exists(joinName) Then joinList.Add(joinName)
  27.         joinIndex += 1
  28.     End While
  29.  
  30.     Dim finalFile(0) As Byte
  31.     For Each file In joinList
  32.         Dim fileBytes() As Byte = IO.File.ReadAllBytes(file)
  33.         Dim currentIndex As Integer = finalFile.Length - 1
  34.         ReDim Preserve finalFile(finalFile.Length + fileBytes.Length - 1)
  35.         For i As Integer = 0 To fileBytes.Length - 1
  36.             finalFile(currentIndex + i) = fileBytes(i)
  37.         Next
  38.     Next
  39.  
  40.     IO.File.WriteAllBytes(IO.Path.Combine(IO.Path.GetDirectoryName(path), IO.Path.GetFileNameWithoutExtension(path)), finalFile)
  41. End Sub
  42.  
  43. Private Sub SplitFile(ByVal path As String, ByVal parts As Integer)
  44.     If Not IO.File.Exists(path) Then
  45.         Throw New ArgumentException("The file '" & path & "' does not exist!")
  46.         Exit Sub
  47.     End If
  48.  
  49.     Dim fInfo As New IO.FileInfo(path)
  50.  
  51.     If parts > fInfo.Length - 1 Then
  52.         Throw New ArgumentException("The amount of parts specified (" & parts & ") is too big!")
  53.         Exit Sub
  54.     End If
  55.  
  56.     Dim fileContents() As Byte = IO.File.ReadAllBytes(path)
  57.     Dim bytesPerFile As Integer = fileContents.Length / parts
  58.     Dim remainingBytes As Integer = fileContents.Length Mod parts
  59.     Dim currentOffset As Integer = 0
  60.     While currentOffset <= fileContents.Length - 1
  61.         For i As Integer = 1 To parts
  62.             Dim bufferLength As Integer = bytesPerFile
  63.             If i = parts Then
  64.                 bytesPerFile += remainingBytes
  65.                 bufferLength = bytesPerFile
  66.             End If
  67.  
  68.             Dim byteBuffer(bufferLength - 1) As Byte
  69.             For index As Integer = 0 To bufferLength - 1
  70.                 byteBuffer(index) = fileContents(currentOffset + index)
  71.             Next
  72.  
  73.             Dim finalPath As String = path & "." & i.ToString("D3")
  74.             IO.File.WriteAllBytes(finalPath, byteBuffer)
  75.             currentOffset += bytesPerFile
  76.             If i = parts Then currentOffset = fInfo.Length
  77.         Next
  78.     End While
  79. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement