推荐期刊

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

时间:2015-12-21 01:45:14 所属分类:计算机技术 浏览量:

在设计信息管理程序时,数据库的软盘备份与恢复是我们经常遇到的一个问题。如果仅仅使用文件复制指令,那么在文件大小超过软盘容量时,就会出现错误。下面我来介绍一种用VB实现的对文件进行分割备份的方法。假设在E:\CLASS目录下有一个名为CLASSES.MDB的文件

在设计信息管理程序时,数据库的软盘备份与恢复是我们经常遇到的一个问题。如果仅仅使用文件复制指令,那么在文件大小超过软盘容量时,就会出现错误。下面我来介绍一种用VB实现的对文件进行分割备份的方法。假设在E:\CLASS目录下有一个名为CLASSES.MDB的文件,3.8M,我们现在要按照每个1.4M,把它分割成3个文件进行备份,然后,再恢复成CLASSES1.MDB文件。启动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\classes.mdb"

Split = 1400000

FilesLen = FileLen(SplitFileName)

fNum = FreeFile

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

NumFil = CInt(FilesLen / Split)

ReDim CurrentFile.Files(1 To NumFil)

For i = 1 To NumFil

ReDim CurrentFile.Files(i).Bytes(1 To Split)

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

Next

For i = 1 To NumFil

Get #fNum, , CurrentFile.Files(i).Bytes

Next

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

CurrentFile.NumberOfFiles = NumFil

Get #fNum, , CurrentFile.Files(NumFil).Bytes

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


Close #fNum

For i = 1 To CurrentFile.NumberOfFiles '分割备份

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

fNum = FreeFile

Open SaveName For Binary As fNum

Put #fNum, 1, CurrentFile.Files(i)

Close #fNum

Next

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

FileInfoFile.FileCount = NumFil

FileInfoFile.OrigFileName = SplitFileName

FileInfoFile.OrigProjSize = FileLen(SplitFileName)

FileInfoFile.FileStartNum = 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\classes.mdb.tpl"

OutName = "e:\class\classes1.mdb"

fNum = FreeFile

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

Get #fNum, , FileInfo

Close #fNum

ReDim File.Files(1 To FileInfo.FileCount) '打开备份文件

For i = 1 To FileInfo.FileCount

OpenName = FileInfo.OrigFileName & "." & Format((FileInfo.FileStartNum - 1 + i), "00#")

fNum = FreeFile

Open OpenName For Binary As #fNum

Get #fNum, 1, File.Files(i)

Close #fNum

Next

fNum = FreeFile

Open OutName For Binary As #fNum '恢复

For i = 1 To FileInfo.FileCount

Put #fNum, , File.Files(i).Bytes

Next

Close #fNum

End Sub

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

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


转载请注明来自:http://www.zazhifabiao.com/lunwen/gcjs/jsjjs/36227.html