最好能提供源码参考!

解决方案 »

  1.   

    未证实-----------------------------------------------------------
    用压缩/恢复数据库方法来实现Access数据库备份:unit unAccessTools;interfaceuses Sysutils,ComObj,Dialogs;function DaoActive(var DaoObject:OleVariant):Boolean;
    function DaoCompactDB(const FileName:string):Boolean;
    function DaoRepairDB(const FileName:string):Boolean;implementationfunction DaoActive(var DaoObject:OleVariant):Boolean;
    begin
      Result:=False;
      try
        DaoObject:=GetActiveOleObject('DAO.DBEngine.36');
        Result:=True;
      except
        try
          DaoObject:=CreateOleObject('DAO.DBEngine.36');
          Result:=True;
        except
          DaoObject:=Null;
        end;
      end;
    end;//压缩Access数据库
    function DaoCompactDB(const FileName:string):Boolean;
    var
      db:OleVariant;
      TempFile:string;
    begin
      Result:=False;
      try
        if not DaoActive(db) then
          Exit;
        try
          TempFile:=ExtractFilePath(FileName)+'msaTemp.mdb';
          db.CompactDatabase(FileName,TempFile);
          DeleteFile(FileName);
          RenameFile(TempFile,FileName);
          Result:=True;
        except
          on E:EOleException do
            ShowMessage(E.Message);
        end
      finally
        db:=Unassigned;
      end;
    end;//修复Access数据库
    function DaoRepairDB(const FileName:string):Boolean;
    var
      db:OleVariant;
    begin
      Result:=False;
      try
        if not DaoActive(db) then
          Exit;
        try
          db.RepairDatabase(FileName);
          Result:=True;
        except
          on E:EOleException do
            ShowMessage(E.Message);
        end
      finally
        db:=Unassigned;
      end;
    end;end.
      

  2.   

    无论压缩、解压,但是者是一个文件的Copy-paster的过程,用FileCopyTO实现。----------------
    沉沦中..........
      

  3.   

    to WWWWA(aaaa):谢谢你!
    我用了 COPYFILE这个函数,可是没有拷过去呀!
      

  4.   

    to  netcha(只喝茶) :谢谢你!
      

  5.   

    to  amei2000go(浪子) :谢谢你!怎么压缩加密,能不能提供例子代码?
      

  6.   

    ////////////////////////////////////////////////////////////////////////////////
    // 功能     : 压缩数据
    // 参数     :
    // CompressionLevel: 压缩比
    // SourceStream: 数据源
    // TargetStream: 目标数据源
    // 返回值   : 是否成功
    // 日期     : 2002/7/12
    ////////////////////////////////////////////////////////////////////////////////
    function CompressStream(const CompressionLevel: TCompressionLevel;SourceStream:TMemoryStream;var TargetStream:TMemoryStream):boolean;
    var
        ComStream:TCompressionStream;
        Count:integer;
    begin
        ComStream:=TCompressionStream.Create(CompressionLevel,TargetStream);
        try
            Count := SourceStream.Size;        SourceStream.SaveToStream(ComStream);
            ComStream.Free;
            SourceStream.Clear;
            //写入原始图像的尺寸
            SourceStream.WriteBuffer(Count, SizeOf(Count));
            //写入经过压缩的图像流
            SourceStream.CopyFrom(TargetStream,0);        TargetStream.Clear;
            SourceStream.SaveToStream(TargetStream);        result:=true;
        except
            result:=false;
        end;
    end;////////////////////////////////////////////////////////////////////////////////
    // 功能     : 解压缩数据
    // 参数     :
    // SourceStream: 数据源
    // TargetStream: 目标数据源
    // 返回值   : 是否成功
    // 日期     : 2002/7/12
    ////////////////////////////////////////////////////////////////////////////////
    function DeCompressStream(SourceStream:TMemoryStream;var TargetStream:TMemoryStream):boolean;
    var
        ComStream:TDecompressionStream;
        TempStream:TMemoryStream;
        FStream:TMemoryStream;
        Count:integer;
        Buffer: PChar;
        B:PChar;
    begin
        FStream:=TMemoryStream.Create;
        TempStream:=TMemoryStream.Create;
        try
            try
                TempStream.LoadFromStream(SourceStream);
                // 将长度去掉
                TempStream.ReadBuffer(Count, SizeOf(Count));            GetMem(B,TempStream.Size-SizeOf(Count));            TempStream.ReadBuffer(B^,TempStream.Size-SizeOf(Count));
                TargetStream.WriteBuffer(B^,TempStream.Size-SizeOf(Count));
                FreeMem(B);            TempStream.Clear;
                TempStream.CopyFrom(TargetStream,0);            TargetStream.Clear;            FStream.LoadFromStream(TempStream);            ComStream:=TDecompressionStream.Create(FStream);            GetMem(Buffer, Count);
                ComStream.ReadBuffer(Buffer^,Count);
                TargetStream.WriteBuffer(Buffer^, Count);
                TargetStream.Position := 0;//复位流指针            FreeMem(Buffer);            result:=true;
            except
                result:=false;
            end;
        finally
            FStream.Free;
            TempStream.Free;
        end;
    end;
      

  7.   

    关于程序的声明,你可以参考Delphi 的光盘,又一个ZLib的示例,你可以看看!上面是我改装的代码。
      

  8.   

    unit DBConnection;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      Db, ADODB, Access2000;type
      TdmDBConnection = class(TDataModule)
        adocAccess: TADOConnection;
        adsGetSysParam: TADODataSet;
        adsCAInfo: TADODataSet;
        procedure DataModuleCreate(Sender: TObject);
        procedure DataModuleDestroy(Sender: TObject);
      private
        { Private declarations }  public
        { Public declarations }    AccessMaster: TAccessApplication;    sDataPath: String;    procedure GetSysParam(const pSection_ID, pIdent: string; var sParam: string);    procedure SetSysParam(const pSection_ID, pIdent: string; sParam: string);    procedure ConnectDB(MDBName: String);    function PackDB(DBFileName: String): String;    function CompactMDB(sSourceDB: String): String;  end;var
      dmDBConnection: TdmDBConnection;implementation
    uses Registry, FileCtrl, JRO_TLB, uSysCommon, uSysSecCom;{$R *.DFM}procedure TdmDBConnection.GetSysParam(const pSection_ID, pIdent: string; var sParam: string);
    begin
      if not adocAccess.Connected then ConnectDB('SysReg.mdb');
      with adsGetSysParam do
      begin
        if Active then
          Close;
        Parameters.ParamByName('Section_ID').Value := pSection_ID;
        Parameters.ParamByName('Ident').Value := pIdent;
        Open;
        if not EOF then
          sParam := FieldByName('Params').AsString
        else
          sParam := EmptyStr;
      //  ShowMessage(sParam);
        Close;
      end;
    end;function TdmDBConnection.PackDB(DBFileName: String): String;
    const
      sProvider = 'Provider=Microsoft.Jet.OLEDB.4.0;';
    var
      IJetEng: JetEngine;
      sOldMDB, sNewMDB, sTempFile, sErr: String;
    begin
      sErr := '';
      sTempFile := ExtractFileDir(Application.ExeName) + '\TmpAccessDB01.mdb';  if FileExists(sTempFile) then DeleteFile(sTempFile);  sOldMDB := sProvider + 'Data Source=' + DBFileName;
      sNewMDB := sProvider + 'Data Source=' + sTempFile;  try
        try
          IJetEng := CoJetEngine.Create;
          IJetEng.CompactDatabase(sOldMDB, sNewMDB);
        //  if FileExists(DBName) then DeleteFile(DBName);
          if not CopyFile(PChar(sTempFile), PChar(DBFileName), False) then
            sErr := 'Fail to overwrite the old database.';
        //  IJetEng := Nil;
        //  Result  := True;
        except
          on E: Exception do
            sErr := E.Message;
        end;
      finally
        IJetEng := Nil;
        if FileExists(sTempFile) then DeleteFile(sTempFile);
        if sErr <> '' then
        begin
          Result := 'Fail to compact database "' +DBFileName+ '".' +
                    'Reason: ' + sErr;
         // MessageDlg(String(Result), mtError, [mbOK], 0);
        end
        else Result := 'Compact database "' +DBFileName+ '" successfully.';
     //   Result  := False;
      end;
    end;procedure TdmDBConnection.DataModuleCreate(Sender: TObject);
    const
      RegPath = '\Software\Tradelink\iTTS';
    var
      Reg : TRegistry;
      sServerPath: String;
      sTemp: String;
    begin  AccessMaster := TAccessApplication.Create(nil);  Reg := TRegistry.Create(KEY_READ);
      try
        Reg.RootKey := HKEY_LOCAL_MACHINE;
        if Reg.OpenKey(RegPath, False) then
        begin
          sServerPath := Reg.ReadString('ServerPath');
        //  LocalPath  := Reg.ReadString('LocalPath');
        //  Local_Version := Reg.ReadString('Version');
        end
        Else raise Exception.Create('Unable to open registry.');
        if (sServerPath = EmptyStr) or (Not DirectoryExists(sServerPath)) then
          raise Exception.Create('Unable to locate the server directory: ''' + sServerPath + '''');
      //  if (LocalPath = EmptyStr) or (Not DirectoryExists(LocalPath)) then
      //    raise Exception.Create('Unable to locate the local directory: ''' + LocalPath + '''');
        if sServerPath[Length(sServerPath)] <> '\' then sServerPath := sServerPath + '\';
        uSysCommon.ServerPath := sServerPath;
      //  if LocalPath[Length(LocalPath)] <> '\' then LocalPath := LocalPath + '\';
      finally
        Reg.CloseKey;
        Reg.Free;
      end;  BasePath := ExtractFilePath(Copy(sServerPath, 1, Length(sServerPath)-1));
      if Length(BasePath) > 0 then
        if BasePath[Length(BasePath)] <> '\' then BasePath := BasePath + '\';  sDataPath := BasePath + 'DATA';  sTemp := '';
      GetSysParam('POLICY', 'LOUTDB', sTemp);
      if sTemp <> '' then uSysSecCom.LOUTDB := sTemp
      else uSysSecCom.LOUTDB := BasePath + 'Sec\Out';  GetSysParam('POLICY', 'LINDB', sTemp);
      if sTemp <> '' then uSysSecCom.LINDB := sTemp
      else uSysSecCom.LINDB := BasePath + 'Sec\In';  ConnectDB('SysReg.mdb');
      
    end;procedure TdmDBConnection.ConnectDB(MDBName: String);
    const
      sProvider = 'Provider=Microsoft.Jet.OLEDB.4.0;';
    begin
      with adocAccess do
      begin
        Connected:= False;
        ConnectionString := sProvider + 'Data Source=' + sDatapath +'\'+ MDBName;
        Connected:= True;
      end;
    end;procedure TdmDBConnection.DataModuleDestroy(Sender: TObject);
    begin
      adsGetSysParam.Active := False;
      adsCAInfo.Active := False;
      adocAccess.Connected := False;
      AccessMaster.Free;
    end;procedure TdmDBConnection.SetSysParam(const pSection_ID, pIdent: string;
      sParam: string);
    begin
      if not adocAccess.Connected then ConnectDB('SysReg.mdb');
      with adsGetSysParam do
      begin
        if Active then
          Close;
        Parameters.ParamByName('Section_ID').Value := pSection_ID;
        Parameters.ParamByName('Ident').Value := pIdent;
        Open;
        if RecordCount > 0 then
        begin
          First;
          Edit;
          FieldByName('Params').AsString := sParam;
          Post;
        end;
     //   else
     //     sParam := EmptyStr;
      //  ShowMessage(sParam);
        Close;
      end;
    end;function TdmDBConnection.CompactMDB(sSourceDB: String): String;
    var
      sErr, sTmpFile: String;begin
      sTmpFile := ExtractFileDir(Application.ExeName) + '\TmpAccessDB01.mdb';  if FileExists(sTmpFile) then DeleteFile(sTmpFile);
      Application.ProcessMessages;
      
      Result := 'Cannot compact the database. Reason: ';
      sErr := ''; // AccessMaster := TAccessApplication.Create(nil);
      try
        try
          AccessMaster.DBEngine.CompactDatabase(WideString(sSourceDB), WideString(sTmpFile),
                                                EmptyParam, EmptyParam, EmptyParam);
          Application.ProcessMessages;
          if not CopyFile(PChar(sTmpFile), PChar(sSourceDB), False) then
          begin
            sErr := 'Fail to overwrite the old database.';
            Abort;
          end
          else Result := 'Compact database "' +sSourceDB+ '" successfully.';
          Application.ProcessMessages;
        finally
        //  AccessMaster.Free;
          
        end;
      except
        on E: Exception do
        begin
        //  if AccessMaster <> nil then AccessMaster.Free;
          if sErr <> '' then
            Result := Result + sErr
          else
            Result := Result + E.Message;
        end;
      end;end;end.
      

  9.   

    to wlw88(飞扬):谢谢你!能给一个直接拷贝的例子么?简单点的为好。
    to amei2000go(浪子):谢谢你!这两个函数直接害现了备份与恢复的功能了吗?
    to Wally_wu(韦利):谢谢你!我调试时出现了很多问题,我现在头都晕了。 
      

  10.   

    CopyFile(Pchar('c:\from.txt'),Pchar('to.txt'),true);
      

  11.   

    CopyFile(Pchar('c:\from.txt'),Pchar('d:\to.txt'),true);
      

  12.   

    darkhorsedba(老牛) :谢谢你!
     qwertyasd(昊) :谢谢你!
      

  13.   

    谢谢各位的帮助!用简单的方法实现了(CopyFile),结贴!