用VB实现对库文件的分割备份
作者:佚名; 更新时间:2014-12-05
在设计信息管理程序时,数据库的软盘备份与恢复是我们经常遇到的一个问题。如果仅仅使用文件复制指令,那么在文件大小超过软盘容量时,就会出现错误。下面我来介绍一种用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盘等语句,这样,一个实用的备份与恢复程序就完成了。

    核心期刊快速发表
    Copyright@2000-2030 论文期刊网 Corporation All Rights Reserved.
    《中华人民共和国信息产业部》备案号:ICP备07016076号;《公安部》备案号:33010402003207
    本网站专业、正规提供职称论文发表和写作指导服务,并收录了海量免费论文和数百个经国家新闻出版总署审批过的具有国内统一CN刊号与国际标准ISSN刊号的合作期刊,供诸位正确选择和阅读参考,免费论文版权归原作者所有,谨防侵权。联系邮箱:256081@163.com