Option Explicit'功能:文件合并与坼分'成员函数、方法、属性说明: ' 1、CombineFiles 合并指定得文件 ' 2、SplitFiles 坼分指定得文件 ' 3、GetFileFullName 返回一个文件的名称 ' 4、LastError 返回最近一次错误编号'文件存储结构如下: '___________________________________________ ' 1、lngFileLen As Long '记录文件长度 ' 2、lngFileNameLen As Long '记录文件名长度 ' 3、strFileName() As Byte '记录文件名 ' 4、btFileData() As Byte '文件数据 '___________________________________________Private lngLastErrNum As Long '记录最近一次错误得编号'返回最近一次错误编号 Public Property Get LastError() As Long LastError = lngLastErrNum End Property'合并文件 Public Function CombineFiles(ByRef strFiles() As String, ByVal strOutFile As String) As String '参数说明: ' 1、strFiles 被合并得文件名数组 ' 2、strOutFile 合并后得文件名 ' '函数返回: ' 1、成功:返回strOutFile ' 2、失败:返回 "",并设置LastError
Dim btArrData() As Byte '存储被合并得单个文件得数据 Dim btFileName() As Byte '存储文件名 Dim lngFLen As Long '文件长度 Dim lngFNameLen As Long '文件名长度 Dim I As Long, K As Long, lFNSource As Long, lFNDest As Long
On Error GoTo Err1 K = UBound(strFiles) lFNDest = FreeFile Open strOutFile For Binary As #lFNDest For I = 0 To K lFNSource = FreeFile Open strFiles(I) For Binary As #lFNSource lngFLen = LOF(lFNSource) '取出文件长度 btFileName = GetFileFullName(strFiles(I)) '取得文件名 lngFNameLen = UBound(btFileName) + 1 '取出文件名长度,Unicode长度 ReDim btArrData(lngFLen - 1) As Byte Get #lFNSource, , btArrData '读取文件数据 Put #lFNDest, , lngFLen '保存文件长度 Put #lFNDest, , lngFNameLen '保存文件名长度 Put #lFNDest, , btFileName '保存文件名 Put #lFNDest, , btArrData '保存文件数据 Close #lFNSource Next Close #lFNDest CombineFiles = strOutFile Exit Function Err1: CombineFiles = "" lngLastErrNum = Err.Number End Function'坼分文件 Public Function SplitFiles(ByVal strSourceFile As String, ByVal strOutPath As String, ByRef strOutFiles() As String) As Long '参数说明: ' 1、strSourceFile 源文件,合并得文件 ' 2、strOutFiles() 返回坼分得文件名列表 '函数返回: ' 1、成功:返回 坼分后得文件数目 ' 2、失败:返回 0,并设置LastError
Dim lngFCount As Long '坼分得文件数目 Dim lFNSource As Long, lFNDest As Long Dim btArrData() As Byte '存储被合并得单个文件得数据 Dim btFileName() As Byte '存储文件名 Dim lngFLen As Long '文件长度 Dim lngFNameLen As Long '文件名长度 Dim strTmp As String, strPath As String
If Right(strOutPath, 1) <> "\" Then strPath = strOutPath & "\" Else strPath = strOutPath End If lFNSource = FreeFile lngFCount = 0 Open strSourceFile For Binary As #lFNSource lFNDest = FreeFile Do Until EOF(lFNSource) Get #lFNSource, , lngFLen '取出文件长度 Get #lFNSource, , lngFNameLen '取出文件名长度 If lngFNameLen > 0 Then ReDim btFileName(lngFNameLen - 1) As Byte ReDim btArrData(lngFLen - 1) As Byte Get #lFNSource, , btFileName '取出文件名 Get #lFNSource, , btArrData '取出文件数据
'开始写文件______________________________ lFNDest = FreeFile strTmp = btFileName strTmp = strPath & strTmp Open strTmp For Binary As #lFNDest Put #lFNDest, , btArrData '输出文件数据 Close #lFNDest ReDim Preserve strOutFiles(lngFCount) As String strOutFiles(lngFCount) = strTmp lngFCount = lngFCount + 1 Else Exit Do End If Loop Close #lFNSource SplitFiles = lngFCount Exit Function Err1: lngFCount = 0 lngLastErrNum = Err.Number End Function'返回文件得名称 Private Function GetFileFullName(ByVal strPath As String) As String Dim intPos As Integer
intPos = InStrRev(strPath, "\") If intPos > 0 Then GetFileFullName = Mid(strPath, intPos + 1) Else GetFileFullName = "" End If End Function
' 1、CombineFiles 合并指定得文件
' 2、SplitFiles 坼分指定得文件
' 3、GetFileFullName 返回一个文件的名称
' 4、LastError 返回最近一次错误编号'文件存储结构如下:
'___________________________________________
' 1、lngFileLen As Long '记录文件长度
' 2、lngFileNameLen As Long '记录文件名长度
' 3、strFileName() As Byte '记录文件名
' 4、btFileData() As Byte '文件数据
'___________________________________________Private lngLastErrNum As Long '记录最近一次错误得编号'返回最近一次错误编号
Public Property Get LastError() As Long
LastError = lngLastErrNum
End Property'合并文件
Public Function CombineFiles(ByRef strFiles() As String, ByVal strOutFile As String) As String
'参数说明:
' 1、strFiles 被合并得文件名数组
' 2、strOutFile 合并后得文件名
'
'函数返回:
' 1、成功:返回strOutFile
' 2、失败:返回 "",并设置LastError
Dim btArrData() As Byte '存储被合并得单个文件得数据
Dim btFileName() As Byte '存储文件名
Dim lngFLen As Long '文件长度
Dim lngFNameLen As Long '文件名长度
Dim I As Long, K As Long, lFNSource As Long, lFNDest As Long
On Error GoTo Err1
K = UBound(strFiles)
lFNDest = FreeFile
Open strOutFile For Binary As #lFNDest
For I = 0 To K
lFNSource = FreeFile
Open strFiles(I) For Binary As #lFNSource
lngFLen = LOF(lFNSource) '取出文件长度
btFileName = GetFileFullName(strFiles(I)) '取得文件名
lngFNameLen = UBound(btFileName) + 1 '取出文件名长度,Unicode长度
ReDim btArrData(lngFLen - 1) As Byte
Get #lFNSource, , btArrData '读取文件数据
Put #lFNDest, , lngFLen '保存文件长度
Put #lFNDest, , lngFNameLen '保存文件名长度
Put #lFNDest, , btFileName '保存文件名
Put #lFNDest, , btArrData '保存文件数据
Close #lFNSource
Next
Close #lFNDest
CombineFiles = strOutFile
Exit Function
Err1:
CombineFiles = ""
lngLastErrNum = Err.Number
End Function'坼分文件
Public Function SplitFiles(ByVal strSourceFile As String, ByVal strOutPath As String, ByRef strOutFiles() As String) As Long
'参数说明:
' 1、strSourceFile 源文件,合并得文件
' 2、strOutFiles() 返回坼分得文件名列表
'函数返回:
' 1、成功:返回 坼分后得文件数目
' 2、失败:返回 0,并设置LastError
Dim lngFCount As Long '坼分得文件数目
Dim lFNSource As Long, lFNDest As Long
Dim btArrData() As Byte '存储被合并得单个文件得数据
Dim btFileName() As Byte '存储文件名
Dim lngFLen As Long '文件长度
Dim lngFNameLen As Long '文件名长度
Dim strTmp As String, strPath As String
If Right(strOutPath, 1) <> "\" Then
strPath = strOutPath & "\"
Else
strPath = strOutPath
End If
lFNSource = FreeFile
lngFCount = 0
Open strSourceFile For Binary As #lFNSource
lFNDest = FreeFile
Do Until EOF(lFNSource)
Get #lFNSource, , lngFLen '取出文件长度
Get #lFNSource, , lngFNameLen '取出文件名长度
If lngFNameLen > 0 Then
ReDim btFileName(lngFNameLen - 1) As Byte
ReDim btArrData(lngFLen - 1) As Byte
Get #lFNSource, , btFileName '取出文件名
Get #lFNSource, , btArrData '取出文件数据
'开始写文件______________________________
lFNDest = FreeFile
strTmp = btFileName
strTmp = strPath & strTmp
Open strTmp For Binary As #lFNDest
Put #lFNDest, , btArrData '输出文件数据
Close #lFNDest
ReDim Preserve strOutFiles(lngFCount) As String
strOutFiles(lngFCount) = strTmp
lngFCount = lngFCount + 1
Else
Exit Do
End If
Loop
Close #lFNSource
SplitFiles = lngFCount
Exit Function
Err1:
lngFCount = 0
lngLastErrNum = Err.Number
End Function'返回文件得名称
Private Function GetFileFullName(ByVal strPath As String) As String
Dim intPos As Integer
intPos = InStrRev(strPath, "\")
If intPos > 0 Then
GetFileFullName = Mid(strPath, intPos + 1)
Else
GetFileFullName = ""
End If
End Function