您好我正在尝试将拆分文件合并在一起。我有这个代码工作,但它很慢。我有什么想法可以加快速度吗?每个文件块最多可包含5mb块,并且可以有100个或更多块可以重新组合在一起。我传递了一个块文件路径“mergeFileNameArray”的数组,以及新合并文件的“filename”。
非常感谢!
Dim STo As System.IO.Stream = System.IO.File.Open _ (filename, _ System.IO.FileMode.Create, _ System.IO.FileAccess.Write) Dim BW As New System.IO.BinaryWriter(STo) For Each F In mergeFileNameArray Dim STi As System.IO.Stream = System.IO.File.Open _ (F, _ System.IO.FileMode.Open, _ System.IO.FileAccess.Read) Dim BR As New System.IO.BinaryReader(STi) Do Until (BR.BaseStream.Position = BR.BaseStream.Length) BW.Write(BR.ReadByte) Loop BR.Close() Next BW.Close()
答案 0 :(得分:2)
一次复制一个字节的文件效率不高。相反,您可以使用缓冲区和FileStream来复制文件块;在它最基本的地方:
Option Infer On
Imports System.IO
Module Module1
Sub ConcatenateFileParts(fileParts As String(), destFile As String)
Using outStream As New FileStream(destFile, FileMode.Create, FileAccess.Write, FileShare.None)
Dim bufferLength = 32768
Dim buffer(bufferLength - 1) As Byte
Dim bytesRead As Integer
For Each inFile In fileParts
Using inStream As New FileStream(inFile, FileMode.Open, FileAccess.Read, FileShare.Read)
bytesRead = inStream.Read(buffer, 0, bufferLength)
While bytesRead > 0
outStream.Write(buffer, 0, bytesRead)
bytesRead = inStream.Read(buffer, 0, bufferLength)
End While
End Using
Next
End Using
End Sub
Sub Main()
' I put a couple of files in "C:\temp\subdir" for testing.
Dim filesToMerge = Directory.GetFiles("C:\temp\subdir")
Dim dest = "C:\temp\merged.txt"
Try
ConcatenateFileParts(filesToMerge, dest)
Catch ex As Exception
MsgBox("File merge failed because " & ex.Message)
End Try
End Sub
End Module
Using
构造确保文件流已关闭并正确处理。
您可以通过报告进度使其更复杂,例如在复制每个文件部分时递增计数器。