欢迎来到学术参考网

用VB实现对库文件的分割备份

发布时间:2015-07-09 11:02
在设计信息管理程序时,数据库的软盘备份与恢复是我们经常遇到的一个问题。如果仅仅使用文件复制指令,那么在文件大小超过软盘容量时,就会出现错误。下面我来介绍一种用VB实现的对文件进行分割备份的方法。假设在E:“CLASS目录下有一个名为的文件,3.8M,我们现在要按照每个1.4M,把它分割成3个文件进行备份,然后,再恢复成文件。启动VB,新建一个工程,添加一个模块,将下列代码填入模块的声明中:

Type FileSection

Bytes() As Byte

FileLen As Long

End Type

Type SectionedFile

Files() As FileSection

NumberOfFiles As Long

End Type

Type FileInfo

OrigProjSize As Long

OrigFileName As String

FileCount As Integer

FileStartNum As Long

End Type

然后,在Form1中添加两个按钮,分别命名为cmdBackup,Caption:"备份";cmdRestore,Caption:"恢复"。

分别双击按钮,填入以下代码:

Private Sub cmdBackup_Click()

Dim SplitFileName As String ‘待备份的文件名

Dim Split As Long ‘备份文件的大小

Dim SaveName As String ‘备份文件名

Dim fNum As Integer ‘可用的文件号

Dim NumFil As Long ‘备份文件的数目

Dim FilesLen As Long ‘待备份文件的大小

Dim CurrentFile As SectionedFile ‘参数结构

SplitFileName = "e:“class“"

Split = 1400000

FilesLen = FileLen(SplitFileName)

fNum = FreeFile

Open SplitFileName For Binary As fNum ‘计算备份参数

NumFil = CInt(FilesLen / Split)

ReDim (1 To NumFil)

For i = 1 To NumFil

ReDim (i).Bytes(1 To Split)

(i).FileLen = UBound((i).Bytes)

Next

For i = 1 To NumFil

Get #fNum, , (i).Bytes

Next

ReDim (NumFil).Bytes(1 To FilesLen - ((NumFil - 1) * Split))

OfFiles = NumFil

Get #fNum, , (NumFil).Bytes

(NumFil).FileLen = UBound((NumFil).Bytes)

Close #fNum

For i = 1 To OfFiles ‘分割备份

SaveName = SplitFileName & "." & Format(BeginningNumber - 1 + i, "00#")

fNum = FreeFile

Open SaveName For Binary As fNum

Put #fNum, 1, (i)

Close #fNum

Next

Dim FileInfoFile As FileInfo ‘相关数据写入临时文件

unt = NumFil

leName = SplitFileName

ojSize = FileLen(SplitFileName)

artNum = BeginningNumber

SaveName = SplitFileName & ".tpl"

fNum = FreeFile

Open SaveName For Binary As #fNum

Put #fNum, , FileInfoFile

Close #fNum

End Sub

Private Sub cmdRestore_Click()

Dim OutName As String ‘恢复后文件名

Dim fNum As Integer ‘可用的文件号

Dim tmpFileName As String ‘临时文件名

Dim File As SectionedFile ‘参数结构

Dim FileInfo As FileInfo ‘临时文件的结构

tmpFileName = "e:“class“"

OutName = "e:“class“"

fNum = FreeFile

Open tmpFileName For Binary As #fNum ‘取得相关数据

Get #fNum, , FileInfo

Close #fNum

ReDim (1 To unt) ‘打开备份文件

For i = 1 To unt

OpenName = leName & "." & Format((artNum - 1 + i), "00#")

fNum = FreeFile

Open OpenName For Binary As #fNum

Get #fNum, 1, (i)

Close #fNum

Next

fNum = FreeFile

Open OutName For Binary As #fNum ‘恢复

For i = 1 To unt

Put #fNum, , (i).Bytes

Next

Close #fNum

End Sub

运行该例程,点击“备份”按钮,用资源管理器看看E:“CLASS目录,可以看到分割后的三个文件:.000、.001、.002,大小分别为:1.33M、1.33M、1.1M,还有一个存放参数的文件:,大小:1K。点击“恢复”按钮,这些文件又被恢复成了。

以上程序在PWIN98、VB6环境下运行通过。需要的朋友可以加入拷贝到A盘等语句,这样,一个实用的备份与恢复程序就完成了。

上一篇:浅谈银行计算机网络系统的安全防范

下一篇:Java语言与Java 技术