?? adobackup.pas
字號:
RemoveDir(FTempPath);
Result := True;
end;
function TADOBackup.CopytoTemp(SourcePath: string): Boolean;
var
FindResult: Integer;
mSearchRec: TSearchRec;
tmpSource, tmpTarget, TempFileName: string;
mReadFile: file of TFloppyFileHead;
mFloppyFileHead: TFloppyFileHead;
begin
Result := False;
FindResult := FindFirst(SourcePath + '*.*', faAnyFile + faHidden + faSysFile, mSearchRec);
try
while FindResult = 0 do
begin
TempFileName := mSearchRec.Name;
if Copy(mSearchRec.Name, 1, Pos('.', FBackUpFileName) - 1) <>
Copy(FBackUpFileName, 1, Pos('.', FBackUpFileName) - 1) then
begin
FindResult := FindNext(mSearChRec);
Continue;
end;
tmpSource := SourcePath + TempFileName; // 從工作路徑里提出要拷貝的分割后的文件
Assignfile(mReadFile, tmpSource);
ReSet(mReadFile);
try
if not Eof(mReadFile) then
Read(mReadFile, mFloppyFileHead);
if mFloppyFileHead.ID = 'JSFB' then
if mFloppyFileHead.SerialNo = FFloppyFileHead.SerialNo then
FFileHeadList[mFloppyFileHead.CurFloppyNo - 1] := mFloppyFileHead;
finally
CloseFile(mReadFile);
end;
tmpTarget := FTempPath + TempFileName;
if not CopyFile(PChar(tmpSource), PChar(tmpTarget), False) then
raise Exception.Create('拷貝失敗!');
FindResult := FindNext(mSearChRec);
end;
finally
findclose(mSearchRec);
end;
end;
function TADOBackup.CopyfromAtoTemp: Boolean; // 將文件從A轉移至臨時目錄
var
FloppyCount, CurFloppy: integer;
SerialNos: Double;
SFile: string;
tmpS: string;
tempFileName: string;
Excursion: Byte;
ErrorFlag: Boolean;
// ErrorCodes: DWord;
FHandle: Integer;
begin
Result := True;
ErrorFlag := False;
// 初始化包信息
FloppyCount := 1; // 包個數
CurFloppy := 1; // 當前包
SerialNos := -1; // 包序列號
SetLength(FFileHeadList, 1);
SFile := StringReplace(FBackUpFileName, '.', InttoStr(1) + '.', [rfReplaceAll, rfIgnoreCase]);
SFile := extractFilePath(FSourceFile) + SFile;
while True do
begin
if UpperCase(Copy(SFile, 1, 1)) = 'A' then // 提示插入磁盤
begin
if CurFloppy <> 1 then
begin
if Application.MessageBox(PChar('請在 A:\ 驅中放入 ' + IntToStr(CurFloppy) +
' 號數據盤。'), '恢復數據', MB_YESNO + MB_ICONWARNING) = ID_NO then
begin
tmpS := '取消操作';
FErrorType := etCancel;
ErrorFlag := False;
Break;
end;
end;
end;
TempFileName := ExtractFileName(SFile);
Excursion := Length(inttostr(CurFloppy)) + 1; // 計算.偏移位數
if CurFloppy = 1 then
SFile := ExtractFilePath(SFile) + Copy(TempFileName, 1, pos('.', TempFileName) - Excursion)
+ ExtractFileExt(SFile)
else if CurFloppy = 2 then
SFile := ExtractFilePath(SFile) + Copy(TempFileName, 1, pos('.', TempFileName) - 1)
+ Inttostr(CurFloppy) + ExtractFileExt(SFile)
else
SFile := ExtractFilePath(SFile) + Copy(TempFileName, 1, pos('.', TempFileName) - Excursion)
+ Inttostr(CurFloppy) + ExtractFileExt(SFile);
if FileExistS(SFile) then
begin
FHandle := FileOpen(SFile, fmOpenRead or fmShareDenyNone);
if FHandle = -1 then
begin
tmpS := '讀備份數據 ' + IntToStr(CurFloppy) + ' 號盤錯誤, ';
ErrorFlag := True;
end;
//將文件抬頭讀入 FFileHeadList
if FileRead(FHandle, FFileHeadList[CurFloppy - 1], Sizeof(FFileHeadList[CurFloppy - 1])) = -1 then
begin
tmpS := '讀備份數據 ' + IntToStr(CurFloppy) + ' 號盤錯誤, ';
ErrorFlag := True;
end;
FileClose(FHandle);
if not ErrorFlag then
begin
if (SerialNos <> FFileHeadList[CurFloppy - 1].SerialNo) and
(SerialNos <> -1) then
begin
tmpS := '備份數據 ' + IntToStr(CurFloppy) + ' 號盤序列號錯誤, ';
ErrorFlag := True;
end;
if not ErrorFlag then
begin
if CurFloppy <> FFileHeadList[CurFloppy - 1].CurFloppyNo then
begin
tmpS := '不是備份數據 ' + IntToStr(CurFloppy) + ' 號盤, ';
ErrorFlag := True;
end
else
begin
// 讀取備份包文件個數,并賦給 FFileHeadList 個數
FloppyCount := FFileHeadList[CurFloppy - 1].FloppyCount;
if CurFloppy = 1 then // 只賦值一次即可
SetLength(FFileHeadList, FloppyCount);
if SerialNos = -1 then // 只賦值一次即可,作為全局參照
begin
SerialNos := FFileHeadList[CurFloppy - 1].SerialNo;
end;
end;
end;
end;
end
else
begin
tmpS := '沒有發現備份數據 ' + IntToStr(CurFloppy) + ' 號盤, ';
ErrorFlag := True;
end;
if not ErrorFlag then
begin
if not CopyFile(PChar(SFile), PChar(fTempPath + ExtractFileName(SFile)), False) then
ErrorFlag := True;
end;
if ErrorFlag then
begin
if Application.MessageBox(PChar('拷貝失敗: ' + tmpS + '重新拷貝嗎?'),
'錯誤', MB_OKCANCEL + MB_ICONWARNING) = mrCancel then
begin
FErrorType := etCancel;
Break;
end
else
begin
ErrorFlag := False; // 仍對當前包進行循環操作
end;
end
else // 可進行下個包的拷貝
begin
Inc(CurFloppy);
if CurFloppy > FloppyCount then
begin
tmpS := '拷貝完畢';
Break;
end;
end;
end;
if ErrorFlag then
Result := False;
end;
function TADOBackup.Backup(SavePath: string): Boolean;
var
mADODataSet: TADODataSet;
i, InfoLen, TotalCount: integer;
TableStrings: TStrings;
BackUpStream: TFileStream;
fn, InfoText: string;
begin
Result := False;
if StartWrong then
Exit;
if SavePath = '' then
begin
Application.MessageBox('路徑無效,請檢查!', '系統信息', 0 + mb_Iconinformation);
exit;
end;
FSavepath := SavePath;
if FSavepath[Length(FSavepath)] <> '\' then
FSavepath := FSavepath + '\';
if DirectoryExists(FTempPath) then
RemoveDir(FTempPath);
Forcedirectories(FTempPath); // 建立臨時路徑
Forcedirectories(FSavePath); // 建立備份路徑
FSqlStrings.SaveToFile(FTempPath + fn_SQLFile); // 儲存SQL腳本
TableStrings := GetTablename(FSqlStrings, rtTable); // 提取表名用以建立文件
mADODataSet := TADODataSet.Create(nil);
mADODataSet.Connection := FADOConnection;
TableMaps.Clear;
TotalCount := FSqLStrings.Count - 1;
try
for i := 0 to FSQLStrings.Count - 1 do
begin
mADODataSet.Close;
mADODataSet.CommandText := FSQLStrings[i];
mADODataSet.Open;
fn := GenerateTempName(FTempPath);
TableMaps.Add(fn);
mADODataSet.SaveToFile(FTempPath + fn, pfxml);
if TotalCount = 0 then
FRadio := 100
else
FRadio := (100 * i div TotalCount);
if Assigned(FRatioChanged) then
FRatioChanged(Self, fRadio);
end;
TableMaps.SaveToFile(FTempPath + fn_TableMap);
TableStrings.Clear;
TableStrings.Add(FTempPath + '*.*');
if UpperCase(Copy(FSavePath, 1, 1)) = 'A' then
begin
FIsSavetoA := True;
FWorkPath := FTempPath;
FIsDevide := True; // 如是A盤,自動分卷
end
else
begin
FIsSavetoA := False;
FWorkPath := FSavePath;
end;
//!!!!!!!!!!!!!!! ForigPath????????????????
if FIsDevide then
fn := FOrigPath + FBackUpFileName
else
fn := FSavePath + FBackUpFileName;
BackUpStream := TFileStream.Create(fn, fmCreate);
BackUpStream.Size := 0;
// 首先寫入備份標志,表明本文件為備份文件
BackUpStream.Write(BackUpSign[1], length(BackUpSign));
InfoText := FInfo.Text;
InfoLen := Length(InfoText);
// 寫入附加信息長度
BackUpStream.write(InfoLen, sizeof(Integer));
// 寫入附加信息
BackUpStream.write(InfoText[1], InfoLen);
try
BackUpFile.BackupToStream(TableStrings, BackUpStream); // 將多文件壓縮整理為單一文件
finally
BackUpStream.free;
end;
if FIsDevide then
begin
CopyDevide;
DeleteFile(fn);
end
finally
mADODataSet.free;
RemoveDir(FTempPath);
end;
Result := True;
end;
procedure TADOBackup.PreviewFile(Sender: TOpenPreviewDialog; FileName: string);
begin
if GetInfo(FileName) then
begin
Sender.PreviewText.Lines.Assign(FInfo);
Sender.PreviewText.Font.Color := clBlack;
Sender.Hint := '備份數據內容:';
end
else
begin
Sender.Hint := '文件格式錯誤';
Sender.PreviewText.Lines.Clear;
Sender.PreviewText.Lines.Add('無法識別的文件格式:' + #13#10 + ' 此文件不是備份文件');
Sender.PreviewText.Font.Color := clRed;
end;
end;
function TADOBackup.BrowseBackFile: string;
var
OpenDlg: TOpenPreviewDialog;
begin
OpenDlg := TOpenPreviewDialog.Create(self);
OpenDlg.Title := '指定需恢復的備份文件';
OpenDlg.Filter := '數據備份文件(*.*)|*.*';
OpenDlg.OnPreview := PreviewFile;
Result := '';
try
if OpenDlg.Execute then
Result := OpenDlg.FileName;
finally
OpenDlg.Free;
end;
end;
constructor TADOBackup.Create(AOwner: TComponent);
var
TempDir: array[0..255] of Char;
begin
inherited Create(AOwner);
PackSize := 1456000;
FBackupFileName := 'Save.dat';
GetTempPath(255, @TempDir);
FTempPath := StrPas(TempDir) + Extractfilename(FBackUpFileName) + '\';
FOrigPath := StrPas(TempDir) + 'TempBack\';
Forcedirectories(FOrigPath);
FIsSavetoA := False;
BackupFile := TBackupFile.Create(nil);
FSQLStrings := TStringList.Create;
FDelSQLStrings := TStringlist.Create;
FInfo := TStringlist.Create;
TableMaps := TStringlist.Create;
FIsDevide := True;
FAutoDel := True;
FPackSize := 1456000;
end;
destructor TADOBackup.Destroy;
begin
RemoveDir(FOrigPath);
BackupFile.free;
FDelSQLStrings.Free;
FSQLStrings.free;
FInfo.Free;
TableMaps.Free;
inherited Destroy;
end;
initialization
Randomize;
end.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -