Type?FileSection?
Bytes()?As?Byte?
End?Type?’定义实际内存数组?
Type?SectionedFile?
Files()?As?FileSection?
End?Type?’定义辅助内存数组,以备扩展使用?
Type?FileInfo?
OrigProjSize?As?Long?’文件大小?
OrigFileName?As?String?
FileCount?As?Integer?
FileStartNum?As?Long?
End?Type?’定义还原信息文件结构参数?
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?CleanUp?
Dim?CurrentFile?As?SectionedFile,m_lngNumFil?As?Long,m_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?
m_lngNumFil=CInt(FilesLen/Split)?
Else?
m_lngNumFil=CInt(FilesLen/Split)+1?
End?If?’精确计算文件将被分割的个数?
ReDim?CurrentFile.Files(1)?’分配一个内存辅助数组?
For?m_LngLoop=1?To?m_lngNumFil?
?’这个循环用来将文件分割,并且生成分割后的文件?
If?m_LngLoop?
ReDim?CurrentFile.Files(1).Bytes(1?To?Split)?
’?重新分配大小等于分割尺寸的内存数组?
Get?#fnum,,CurrentFile.Files(1).Bytes?
’读取等于分割大小的二进制数据到内存数组?
Else?’如果是最后一次循环?
ReDim?CurrentFile.Files(1).Bytes?To?FilesLen-((m_lngNumFil-1)*Split))?
’重新分配大小等于遗留长度的内存数组?
Get?#fnum,,CurrentFile.Files(1).Bytes?
Close?#fnum?’关闭读取文件通道?
End?If?
SaveName=oName?&?"."?&?Format(BeginningNumber-1+m_LngLoop,"00#")?’计算分割后的文件名,扩展名为00??
fnum1=FreeFile?’得到第二个空闲通道用来写入?
Open?SaveName?For?Binary?As?fnum1?
Put?#fnum1,1,CurrentFile.Files(1)?
DoEvents?
Close?#fnum1?’用二进制方式写入分割后的文件?
Form1.Command6.Caption=FormatPercent(m_LngLoop?/?m_lngNumFil)?’显示简单的进度指示?
Form1.Command5.Caption=SaveName?’显示正在操作的文件名?
Next?
Dim?FileInfoFile?As?FileInfo?’定义还原信息文件内容?
FileInfoFile.FileCount=m_lngNumFil?’分割后文件个数?
FileInfoFile.OrigFileName=oName?’输出文件名?
FileInfoFile.OrigProjSize=FileLen(SplitFileName)?’被分割文件大小?
FileInfoFile.FileStartNum=BeginningNumber?’分割后文件起始编号?
SaveName=oName?&?".HJ"?’还原信息文件名?
fnum=FreeFile?
Open?SaveName?For?Binary?As?#fnum?
Put?#fnum,,FileInfoFile?
Close?#fnum?’写入还原信息文件?
Exit?Function?
CleanUp:?’如果出现错误?
ReturnErrorDes=Err.Description?
SplitFile=False?’返回值为false?
End?Function?
PublicFunctionReassembleFile(Template?FileName?As?String,Optional?UseOldFilenameAsBoolean=True,OutPutName?as?string)As?Boolean?’定义文件还原操作公用过程?
On?Error?GoTo?e?
Form1.MousePointer=11?
Dim?FileInfo?As?FileInfo,outname?As?String,File?As?SectionedFile,m_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.OrigFileName?
Else?
outname=OutPutName?
End?If?’是否自己指定还原后文件名?
ReDim?File.Files(1)?
fnum1=FreeFile?
Open?outname?For?Binary?As?#fnum1?
For?m_LngLoop=1?To?FileInfo.FileCount?
’通过还原信息中记载的文件个数确定读取次数?
OpenName=FileInfo.OrigFileName?&?"."?&?Format((FileInfo.FileStartNum-1+m_LngLoop),"00#")?
’得到分割后生成的文件名称、路径?
fnum=FreeFile?
Open?OpenName?For?Binary?As?#fnum?
Get?#fnum,1,File.Files(1)?
Close?#fnum?’读取?
Put?#fnum1,,File.Files(1).Bytes?’写入?
Form1.Command6.Caption=FormatPercent(m_LngLoop/FileInfo.FileCount)?
Form1.Command5.Caption=OpenName?
Next?
Close?#fnum1?
Form1.MousePointer=0?
Exit?Function?
e:?
MsgBox?"文件操作产生了错误:"?&?Err.Description,vbExclamation?
ReassembleFile=False?
Form1.MousePointer=0?
End?Function?
(三)运行调试:?
首先通过浏览指定被分割文件名、分割后文件大小、分割后文件名或者还原信息文件名、还原后文件名,然后点击开始分割或者开始还原即可,该程序算法严谨,但是,为了保留一定的扩展能力和对以往源代码的兼容性,也存在一些冗余代码,读者尽可以根据自己的喜好进行改进。另外,使用vb5的朋友注意了,本文函数FormatPercent函数只能在vb6中使用,它的作用是将一个表达式转化成百分数,你必须采用其他的变通方法,其他代码全部通用。?
本文地址:http://com.8s8s.com/it/it44179.htm