编程原理和思路
文件分割实际上就是将目标文件用二进制读写的方法,精确地连续保存为合适大小的独立文件的过程。文件还原是文件分割的逆过程,即将分割后的文件严格地按照分割顺序用二进制读写的方法写入同一个文件的过程。
使用双通道技术是因为当被分割的文件较大或者非常大(远大于内存)时,利用单个文件通道定义的内存二进制缓冲数组非常容易造成内存的耗尽而导致操作失败,并且单个文件通道的处理速度和可靠性也不令人满意。
首先使用双通道技术将目标文件分割成指定大小的文件,扩展名为“指定文件名+.源文件扩展名+.分割序号”。分割时,生成一个还原信息文件“被分割文件名.HY”,该文件记录文件分割的有关信息。文件还原时,同样利用双通道技术,按照在还原信息文件中登记的信息,将待还原的文件写入同一个文件中即可。
编程实现
实现分割功能的主要代码如下:
1.分割操作
Public Function SplitFile(SplitFileName As String, BeginningNumber As Long, ReturnErrorDes As String, Split As Long, oName As String) As Boolean
/*分割文件函数返回一个逻辑值,true代表成功,false代表失败。参数依次是:被分割文件名、起始编号、返回错误信息、分割后文件大小、分割后文件名称*/
Dim SaveName As String
/*文件通道变量*/
Dim fnum As Integer, fnum1 As Integer
SplitFile = True /*初始化*/
On Error GoTo error1
Dim CurrentFile As SectionedFile, LngNumFile As Long, LngLoop As Long, FilesLen As Long
/*得到被分割文件大小*/
FilesLen = FileLen(SplitFileName)
If FilesLen <= Split + 1 Then
SplitFile = False
ReturnErrorDes = “被分割文件小于分割后文件大小,需重新设置!”
Exit Function
End If
fnum = FreeFile /*返回第一个空闲通道用来读取文件*/
/*用二进制方式打开被分割文件*/
Open SplitFileName For Binary As fnum
/*精确计算文件将被分割的个数*/
If CInt(FilesLen / Split) >= (FilesLen / Split) OR CInt(FilesLen / Split) = (FilesLen / Split) Then
LngNumFile = CInt(FilesLen / Split)
Else
LngNumFile = CInt(FilesLen / Split) + 1
End If
/*分配一个内存辅助数组*/
ReDim CurrentFile.Files(1)
/*循环将文件分割,并且生成分割后的文件*/
For LngLoop = 1 To LngNumFile
If LngLoop < LngNumFile Then
/*重新分配大小等于分割尺寸的内存数组*/
ReDim CurrentFile.Files(1).Bytes(1 To Split)
/*读取等于分割大小的二进制数据到内存数组*/
Get #fnum, , CurrentFile.Files(1).Bytes
Else
/*重新分配大小等于遗留长度的内存数组*/
ReDim CurrentFile.Files(1).Bytes(1 To FilesLen - ((LngNumFile - 1) * Split))
Get #fnum, , CurrentFile.Files(1).Bytes
Close #fnum
End If
/*计算分割后的文件名,扩展名为00?*/
SaveName = oName & “.” & Format(BeginningNumber - 1 + LngLoop, “000”)
/*得到第二个空闲通道用来写入*/
fnum1 = FreeFile
Open SaveName For Binary As fnum1
/*用二进制方式写入分割后的文件*/
Put #fnum1, 1, CurrentFile.Files(1)
DoEvents
Close #fnum1
Next
/*定义还原信息文件内容*/
Dim FileInfoFile As FileInfo
/*分割后文件个数*/
FileInfoFile.FileCount = LngNumFile
/*输出文件名*/
FileInfoFile.ProjectFileName = oName
/*被分割文件大小*/
FileInfoFile.ProjectSize = FileLen(SplitFileName)
/*分割后文件起始编号*/
FileInfoFile.FileStartNum = BeginningNumber
/*还原以HY为后缀的信息文件名*/
SaveName = oName & “.HY”
fnum = FreeFile
Open SaveName For Binary As #fnum
/*写入还原信息文件
Put #fnum, , FileInfoFile
*/
Close #fnum
Exit Function
error1:
ReturnErrorDes = Err.Description
SplitFile = False
End Function
2.文件还原
Public Function ReassembleFile(TemplateFileName As String, UseOldFilename As Boolean, OutPutName As String) As Boolean
On Error GoTo error2
Dim FileInfo As FileInfo, outname As String, File As SectionedFile, LngLoop As Long, OpenName
Dim fnum As Integer, fnum1 As Integer
ReassembleFile = True
fnum = FreeFile
Open TemplateFileName For Binary As #fnum
/*读取还原信息文件的有关内容*/
Get #fnum, , FileInfo
Close #fnum
/*是否是自己指定还原后文件名*/
If UseOldFilename Then
outname = FileInfo.ProjectFileName
Else
outname = OutPutName
End If
ReDim File.Files(1)
fnum1 = FreeFile
Open outname For Binary As #fnum1
/*通过还原信息中记载的文件个数确定读取次数*/
For LngLoop = 1 To FileInfo.FileCount
/*得到分割后生成的文件名称和路径*/
OpenName = FileInfo.ProjectFileName & “.” & Format((FileInfo.FileStartNum - 1 + LngLoop), “000”)
fnum = FreeFile
Open OpenName For Binary As #fnum
Get #fnum, 1, File.Files(1) /*读取*/
Close #fnum
Put #fnum1, ,File.Files(1).Bytes /*写入*/
Next
Close #fnum1
Exit Function
error2:
MsgBox “文件操作产生错误:” & Err.Description, vbExclamation
ReassembleFile = False
Form1.MousePointer = 0
End Function
延伸阅读
文章来源于领测软件测试网 https://www.ltesting.net/