Einzelnen Beitrag anzeigen

Benutzerbild von Bummi
Bummi

Registriert seit: 15. Jun 2010
Ort: Augsburg Bayern Süddeutschland
3.470 Beiträge
 
Delphi XE3 Enterprise
 
#4

AW: In Datei Suchen und Kopieren

  Alt 12. Jan 2013, 18:04
Ich hätte folgendes anzubieten: ACHTUNG tar ist jetzt 0 basiert.

Delphi-Quellcode:
Const
  tar: Array [0 .. 8] of Byte =($3E, $00, $53, $00, $47, $00, $40, $00, $3E);



function ByteArrayPos(const SearchArr : array of byte; const CompArr : array of byte) : integer;
// Thomas Wassermann
// result=Position or -1 if not found
var
  Comp,Search : AnsiString;
begin
  SetString(Comp, PAnsiChar(@CompArr[0]), Length(CompArr));
  SetString(Search, PAnsiChar(@SearchArr[0]), Length(SearchArr));
  Result := Pos(Search,Comp) - 1;
end;

Function CopyFromToken(Const SrcFile,DestFile:String; const arr:Array of Byte):Boolean;
var
 // Thomas Wassermann
 src,dst:TFileStream;
 Buff:Array of Byte;
 Pos,Size:Integer;
 FoundPos:Integer;
begin
    pos := 0;
    Result := false;
    Size := High(Arr) * 100;
    FoundPos:=-1;
    src:=TFileStream.Create(SrcFile,fmOpenRead);
    try
    if Size > (src.Size - Pos) then Size := src.Size - Pos;
    while (FoundPos<0) and ((Pos+Size)<src.Size) do
        begin
         if High(Buff)+ 1 <> Size then SetLength(Buff,Size);
          src.Read(Buff[0],Size);
          FoundPos := ByteArrayPos(Arr,Buff);
          if (FoundPos<0) and (src.Position < src.Size ) and (src.Size>Size) then
            begin
              src.Position := src.Position - High(arr) - 1;
              Pos := src.Position
            end;
        end;
    if FoundPos > -1 then
        begin
          src.Position := Pos+FoundPos;
          dst := TFileStream.Create(DestFile,fmCreate);
          try
          dst.CopyFrom(src,src.Size-src.Position);
          Result := true;
          finally
            dst.Free;
          end;
        end;
    finally
       src.Free;
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  CopyFromToken('C:\temp\Bild 6.png','C:\temp\2.Bin',tar);
end;
Thomas Wassermann H₂♂
Das Problem steckt meistens zwischen den Ohren
DRY DRY KISS
H₂ (wenn bei meinen Snipplets nichts anderes angegeben ist Lizenz: WTFPL)
  Mit Zitat antworten Zitat