相写个ftp更新程序,功能如下:
1.检索本地文件目录
2.检索ftp服务器文件目录
3.比较差异文件
4.下载差异文件到临时目录
5.从临时目录更新到本地正式目录,并完成日志以上功能,非线程已经实现,想提高效率,采用多线程。
如何实现?
比如第1、2步应该可以同时做,
第3、4、5步也应该可以同时做。
如何实现??? 求教 

解决方案 »

  1.   

    1.检索本地文件目录procedure FindLocalFiles(disk, path: string; var filename, filetime: Tstrings);
    const
      Model = 'yyyy/mm/dd,hh:mm:ss';
    var
      fpath, s: string;
      fs: TsearchRec;
    begin
      fpath := disk + path + '\*.*';
      if FindFirst(fpath, faAnyFile, fs) = 0 then
      begin
        if (fs.Name <> '.') and (fs.Name <> '..') then
          if (fs.Attr and faDirectory) = faDirectory then
            FindLocalFiles(disk, path + '\' + fs.Name, filename, filetime)
          else
          begin
            filename.add(path + '\' + fs.Name);
            filetime.Add(FormatDateTime(Model,
              CovFileDate(fs.FindData.ftLastWriteTime)));
          end;
        while findnext(fs) = 0 do
        begin
          if (fs.Name <> '.') and (fs.Name <> '..') then
            if (fs.Attr and faDirectory) = faDirectory then
              FindLocalFiles(disk, path + '\' + fs.Name, filename, filetime)
            else
            begin
              begin
                filename.add(path + '\' + fs.Name);
                filetime.Add(FormatDateTime(Model,
                  CovFileDate(fs.FindData.ftLastWriteTime)));
              end;
            end;
        end;
      end;
      Findclose(fs);
    end;
      

  2.   

    2.检索ftp服务器文件目录procedure FindRemoteFiles(var idFTP: TIdFtp; RemoteDirectory: string; var
      filename,
      filetime: Tstrings);
    const
      Model = 'yyyy/mm/dd,hh:mm:ss';
    var
      i, DirCount: integer;
      sName: string;
    begin
      idFTP.ChangeDir(RemoteDirectory);
      idFTP.List(nil);
      DirCount := idFTP.DirectoryListing.Count;
      if DirCount = 0 then
      begin
        idFTP.ChangeDirUp;
        idFTP.List(nil);
        Exit;
      end;
      for i := 0 to DirCount - 1 do
      begin
        if DirCount <> idFTP.DirectoryListing.Count then
        begin
          repeat
            idFTP.ChangeDirUp;
            idFTP.List(nil);
            Application.ProcessMessages;
          until DirCount = idFTP.DirectoryListing.Count;
        end;
        if idFTP.DirectoryListing.Items[i].ItemType = ditDirectory then
          //如果是文件夹
          FindRemoteFiles(idFTP, idFTP.RetrieveCurrentDir + '\' +
            idFTP.DirectoryListing.Items[i].FileName, filename, filetime)
        else
        begin
          sName := RemoteDirectory + '\' + idFTP.DirectoryListing.Items[i].FileName;
          sName := MidStr(sName, (length(RemoteDir) + 1) + 1, length(sName) -
            (length(RemoteDir) + 1) + 1);
          filename.add(sName);
          filetime.Add(FormatDateTime(Model,
            idFTP.DirectoryListing.Items[i].ModifiedDate));
        end;
      end;
      Application.ProcessMessages;
      if i = DirCount - 1 then
      begin
        idFTP.ChangeDirUp;
        idFTP.List(nil);
      end;
    end;
      

  3.   

    3.比较差异文件function CheckDifferent(var LName, LTime, RName, RTime: TStrings): TStrings;
    var
      i, j, k, l, m: integer;
      SameFile: Tstrings;
    begin
      SameFile := TstringList.Create;
      for i := 0 to RName.Count - 1 do
      begin
        for j := 0 to LName.Count - 1 do
        begin
          if RName[i] = LName[j] then
            if RTime[i] <= LTime[j] then
              SameFile.Add(RName[i]);
        end;
      end;
      result := RName;
      m := result.Count;
      for k := 0 to SameFile.Count - 1 do
      begin
        for l := 0 to m - 1 do
        begin
          if SameFile[k] = result[l] then
          begin
            result.Delete(l);
            m := m - 1;
            break;
          end;
        end;
      end;
      SameFile.Free;
    end;