Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '==========================
- 'Simple Splitting/Joining code for VB.NET
- 'Created and coded by master131
- '==========================
- 'Usage:
- 'SplitFile("C:\test.exe", 2)
- 'JoinFile("C:\test.exe.001")
- Private Sub JoinFile(ByVal path As String)
- If Not IO.File.Exists(path) Then
- Throw New ArgumentException("The file '" & path & "' does not exist!")
- Exit Sub
- End If
- If Not path.EndsWith(".001") Then
- Throw New ArgumentException("'" & path & "' is not a valid .001 file!")
- Exit Sub
- End If
- Dim joinList As New List(Of String)
- Dim joinIndex As Integer = 1
- Dim joinName As String = IO.Path.Combine(IO.Path.GetDirectoryName(path), IO.Path.GetFileNameWithoutExtension(path) & "." & joinIndex.ToString("D3"))
- While IO.File.Exists(joinName)
- joinName = IO.Path.Combine(IO.Path.GetDirectoryName(path), IO.Path.GetFileNameWithoutExtension(path) & "." & joinIndex.ToString("D3"))
- If IO.File.Exists(joinName) Then joinList.Add(joinName)
- joinIndex += 1
- End While
- Dim finalFile(0) As Byte
- For Each file In joinList
- Dim fileBytes() As Byte = IO.File.ReadAllBytes(file)
- Dim currentIndex As Integer = finalFile.Length - 1
- ReDim Preserve finalFile(finalFile.Length + fileBytes.Length - 1)
- For i As Integer = 0 To fileBytes.Length - 1
- finalFile(currentIndex + i) = fileBytes(i)
- Next
- Next
- IO.File.WriteAllBytes(IO.Path.Combine(IO.Path.GetDirectoryName(path), IO.Path.GetFileNameWithoutExtension(path)), finalFile)
- End Sub
- Private Sub SplitFile(ByVal path As String, ByVal parts As Integer)
- If Not IO.File.Exists(path) Then
- Throw New ArgumentException("The file '" & path & "' does not exist!")
- Exit Sub
- End If
- Dim fInfo As New IO.FileInfo(path)
- If parts > fInfo.Length - 1 Then
- Throw New ArgumentException("The amount of parts specified (" & parts & ") is too big!")
- Exit Sub
- End If
- Dim fileContents() As Byte = IO.File.ReadAllBytes(path)
- Dim bytesPerFile As Integer = fileContents.Length / parts
- Dim remainingBytes As Integer = fileContents.Length Mod parts
- Dim currentOffset As Integer = 0
- While currentOffset <= fileContents.Length - 1
- For i As Integer = 1 To parts
- Dim bufferLength As Integer = bytesPerFile
- If i = parts Then
- bytesPerFile += remainingBytes
- bufferLength = bytesPerFile
- End If
- Dim byteBuffer(bufferLength - 1) As Byte
- For index As Integer = 0 To bufferLength - 1
- byteBuffer(index) = fileContents(currentOffset + index)
- Next
- Dim finalPath As String = path & "." & i.ToString("D3")
- IO.File.WriteAllBytes(finalPath, byteBuffer)
- currentOffset += bytesPerFile
- If i = parts Then currentOffset = fInfo.Length
- Next
- End While
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement