曾经看到过一种方法进行备份和还原:
1。用READ,WRITE,等一些关于文件流操作的函数把一个库中所有的表打包到一个大文件,
其中可加上自己独有的标志字符,以增强保密性。
2。用相反的操作解开!!!
给你的代码参考:
unit BackupUnit;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, StdCtrls, Buttons;
type
TBackupForm = class(TForm)
Label1: TLabel;
Edit1: TEdit;
Button3: TButton;
PBar: TProgressBar;
BackupBBtn: TBitBtn;
RestoreBBtn: TBitBtn;
CancelBBtn: TBitBtn;
procedure Button3Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure BackupBBtnClick(Sender: TObject);
procedure RestoreBBtnClick(Sender: TObject);
procedure CancelBBtnClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
procedure InitPack(ss: string);
procedure PackFile(ss: string);
procedure DepackFile(ss: string);
procedure BackupTo(ss: string);
function ChkDisk(drive: Byte; bbsize: Longint): Boolean;
function ChkFile(ss: string): Boolean;
public
{ Public declarations }
end;
var
BackupForm: TBackupForm;
LastFileSize: Longint;
implementation
uses Math, DirUnit;
{$R *.DFM}
procedure TBackupForm.InitPack(ss: String);
var
vf: File;
begin
AssignFile(vf,ss);
Rewrite(vf,1);
CloseFile(vf);
end;
procedure TBackupForm.PackFile(ss: String);
var
rf,pf: File;
fsize: Longint;
numread,numwrite: Integer;
ss1,ss2,ss3: String[100];
buf: Array[1..2048] of char;
begin
AssignFile(pf, 'c:/FMIS/data/mypack.pak');
AssignFile(rf, ss);
Reset(pf, 1);
Reset(rf, 1);
fsize := FileSize(rf);
Seek(pf, LastFileSize);
LastFileSize := LastFileSize + fsize + 60;
ss1 := 'CHANGE';
ss2 := ss;
Str(fsize, ss3);
BlockWrite(pf, ss1, 10);
BlockWrite(pf, ss2, 30);
BlockWrite(pf, ss3, 20);
repeat
BlockRead(rf, buf, SizeOf(buf), numread);
BlockWrite(pf, buf, numread, numwrite);
until (numread = 0) or (numwrite <> numread);
CloseFile(pf);
CloseFile(rf);
end;
procedure TBackupForm.BackupTo(ss: String);
var
sf,df: File;
nnn,nn,basesize,srcfilesize: Longint;
dnum,vi,bn: Integer;
ss1: String[200];
ssdnum,sste,ssattr: String[20];
vbyte: Char;
buf: Array[1..2048] of Char;
begin
AssignFile(sf, ss);
Reset(sf, 1);
srcfilesize := FileSize(sf);
basesize := 1430000;
dnum := Ceil(srcfilesize / basesize);
Str(dnum, ssdnum);
Randomize;
vi := Random(1000);
Str(vi, ssattr);
for vi := 1 to dnum do
begin
Str(vi, sste);
sste := Trim(sste);
ss1 := '共需' + ssdnum + '张盘,请插入第' + sste + '张盘。';
bn := MessageDlg(ss1,mtInformation,[mbYes,mbNo],0);
if bn <> mrYes then break;
ss1 := UpperCase(Trim(Edit1.Text));
vbyte := ss1[1];
bn := Ord(vbyte) - Ord('A') + 1;
if ChkDisk(bn, basesize) = False then break;
ss1 := Trim(Edit1.Text) + 'xx' + sste + 'disk.bak';
AssignFile(df, ss1);
Rewrite(df, 1);
ss1 := 'xxsoft001';
BlockWrite(df, ss1, 16);
BlockWrite(df, ssdnum, 16);
BlockWrite(df, ssattr, 16);
srcfilesize := basesize;
repeat
if srcfilesize > 2040 then
begin
nn := 2040;
srcfilesize := srcfilesize - 2040;
end
else
begin
nn := srcfilesize;
srcfilesize := 0;
end;
BlockRead(sf, buf, nn, nnn);
BlockWrite(df, buf, nnn);
until(srcfilesize <= 0);
CloseFile(df);
PBar.StepBy(floor(70 / dnum));
end;
CloseFile(sf);
end;
function TBackupForm.ChkDisk(drive: Byte; bbsize: Longint): Boolean;
var
bn,chk: Integer;
mysize: Longint;
begin
Result := True;
chk := 0;
while chk = 0 do
begin
mysize := DiskFree(drive);
if mysize = -1 then
begin
bn := MessageDlg('未插入盘,请插入!',mtWarning,[mbYes,mbNo],0);
if bn = mrYes then continue
else
begin
Result := False;
break;
end;
end
else
begin
if mysize < bbsize then
begin
bn := MessageDlg('磁盘空间不够,请插入另一张!',mtWarning,[mbYes,mbNo],0);
if bn = mrYes then continue
else
begin
Result := False;
break;
end;
end
else break;
end;
end;
end;
procedure TBackupForm.Depackfile(ss: String);
var
sf,df: File;
ss1,ss2,ss3: String[200];
fsize: Longint;
numread,numwrite,nn: Integer;
buf: Array[1..2048] of Char;
begin
AssignFile(sf, ss);
Reset(sf, 1);
while not Eof(sf) do
begin
BlockRead(sf, ss1, 10, nn);
BlockRead(sf, ss2, 30, nn);
BlockRead(sf, ss3, 20, nn);
if nn < 20 then
begin
ShowMessage('文件尺寸不对');
CloseFile(sf);
exit;
end;
fsize := StrToInt(ss3);
if ss1 <> 'CHANGE' then
begin
ShowMessage('标志不对');
CloseFile(sf);
exit;
end;
AssignFile(df, ss2);
Rewrite(df, 1);
repeat
if fsize > 2000 then
begin
nn := 2000;
fsize := fsize - 2000;
end
else
begin
nn := fsize;
fsize := 0;
end;
BlockRead(sf, buf, nn, numread);
BlockWrite(df, buf, numread, numwrite);
until (fsize = 0) or (numwrite <> numread);
CloseFile(df);
end;
CloseFile(sf);
end;
function TBackupForm.ChkFile(ss: String): Boolean;
var
bn: Integer;
begin
Result := True;
while FileExists(ss) = False do
begin
bn := MessageDlg('备份文件不存在!继续吗?', mtWarning, [mbYes, mbNo], 0);
if bn <> mrYes then
begin
Result := False;
break;
end;
end;
end;
procedure TBackupForm.Button3Click(Sender: TObject);
begin
DirForm := DirUnit.TDirForm.Create(Application);
DirForm.Hide;
DirForm.ShowModal;
end;
procedure TBackupForm.Button2Click(Sender: TObject);
begin
Application.Terminate;
end;
procedure TBackupForm.FormCreate(Sender: TObject);
begin
LastFileSize := 0;
end;
procedure TBackupForm.BackupBBtnClick(Sender: TObject);
begin
PBar.Visible := True;
PBar.Min := 0;
PBar.Max := 100;
PBar.Step := 1;
InitPack('c:/FMIS/Data/mypack.pak');
PBar.StepBy(10);
PackFile('c:/FMIS/Data/UserInfo.db');
PBar.StepBy(10);
PackFile('c:/FMIS/Data/FeeFlow.db');
PBar.StepBy(10);
PackFile('c:/FMIS/Data/FeeDetail.db');
PBar.StepBy(10);
BackupTo('c:/FMIS/Data/mypack.pak');
PBar.visible := False;
CancelBBtn.SetFocus;
end;
procedure TBackupForm.RestoreBBtnClick(Sender: TObject);
var
sf,df: File;
ss: String[200];
ss1,ssattr: String[30];
numread,numwrite,bn,nn: Integer;
buf: Array[1..2048] of Char;
begin
ss := Trim(Edit1.text) + 'xx1disk.bak';
if FileExists(ss) = False then
begin
MessageDlg('备份文件不存在!', mtWarning, [mbOk], 0);
exit;
end;
PBar.Visible := True;
AssignFile(df, 'c:/FMIS/data/mypack.pak');
Rewrite(df, 1);
AssignFile(sf, ss);
Reset(sf, 1);
BlockRead(sf, ss1, 16);
if ss1 <> 'xxsoft001' then
begin
MessageDlg('不是该系统的备份文件!', mtWarning, [mbOk], 0);
CloseFile(sf);
exit;
end;
BlockRead(sf, ss1, 16);
nn := StrToInt(ss1);
BlockRead(sf, ssattr, 16);
repeat
BlockRead(sf, buf, SizeOf(buf), numread);
BlockWrite(df, buf, numread, numwrite);
until (numread = 0) or (numwrite <> numread);
CloseFile(sf);
PBar.StepBy(Floor(70 / nn));
if nn > 1 then
begin
for bn := 2 to nn do
begin
MessageDlg('请插入第' + IntToStr(bn) + '张盘!', mtInformation, [mbOk], 0);
ss := Trim(Edit1.text) + 'xx' + Trim(IntToStr(bn)) + 'disk.bak';
if ChkFile(ss) = False then break;
AssignFile(sf, ss);
Reset(sf, 1);
BlockRead(sf, ss1, 16);
if ss1 <> 'xxsoft001' then
begin
MessageDlg('不是该系统的备份文件!', mtWarning, [mbOk], 0);
CloseFile(sf);
CloseFile(df);
exit;
end;
BlockRead(sf, ss1, 16);
BlockRead(sf, ss1, 16);
if ss1 <> ssattr then
begin
MessageDlg('不是同一次的备份文件!', mtWarning, [mbOk], 0);
CloseFile(sf);
CloseFile(df);
exit;
end;
repeat
BlockRead(sf, buf, Sizeof(buf), numread);
BlockWrite(df, buf, numread, numwrite);
until(numread = 0)or(numwrite <> numread);
CloseFile(sf);
PBar.StepBy(Floor(70 / nn));
end;
end;
CloseFile(df);
DepackFile('c:/FMIS/data/mypack.pak');
PBar.StepBy(30);
PBar.Visible := false;
CancelBBtn.SetFocus;
end;
procedure TBackupForm.CancelBBtnClick(Sender: TObject);
begin
BackupForm.Release;
end;
procedure TBackupForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caFree;
end;
end.