Ostatecznie wczoraj poradziłem sobie z tym problemem w trochę bardziej skomplikowany sposób niż to zaproponował pelsta.
function TActivationThread.ModifyInputFilePathInIndexFile(InputFile:string):boolean;
var StringList:TStringList;
SourceFileHandle,TargetFileHandle:THandle;
SourceStream,TargetStream:TFileStream;
DataArray:array of byte;
PositionInFile,NullPos:int64;
PositionInArray:integer;
BytesRead,BytesWritten,x:integer;
TextLine,CurrentDomainName,CurrentJob,OutputFile:string;
function FindNullPos(var Buffer:array of byte):integer;
var x:integer;
begin
Result:=-1;
for x:=0 to Length(Buffer)-1 do
begin
if Buffer[x]=0 then
begin
Result:=x;
exit;
end;
end;
end;
begin
Result:=false;
if FileExists(InputFile)=false then exit;
CurrentDomainName:=form1.JvComputerInfoEx1.Identification.LocalComputerName;
CurrentJob:=Copy(CurrentJobFolder,Pos('job',CurrentJobFolder),MaxInt);
OutputFile:=CurrentJobFolder+'\video.mkv.lwi.tmp';
StringList:=TStringList.Create;
StringList.LoadFromFile(InputFile);
for x:=0 to StringList.Count-1 do
begin
TextLine:=StringList.Strings[x];
if Pos('<InputFilePath>',TextLine)>0 then
begin
StringList.Strings[x]:='<InputFilePath>\\'+CurrentDomainName+'\RipBot264temp\'+CurrentJob+'\video.mkv</InputFilePath>';
break;
end;
end;
StringList.SaveToFile(OutputFile);
StringList.Free;
SourceFileHandle:=CreateFile(PChar(InputFile), GENERIC_READ, FILE_SHARE_READ, nil,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
TargetFileHandle:=CreateFile(PChar(OutputFile), GENERIC_WRITE, FILE_SHARE_WRITE, nil,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if SourceFileHandle=INVALID_HANDLE_VALUE then exit;
SourceStream:=TFileStream.Create(SourceFileHandle);
TargetStream:=TFileStream.Create(TargetFileHandle);
TargetStream.Seek(0,soFromEnd);
SetLength(DataArray,16777216);
try
repeat
PositionInFile:=SourceStream.Position;
BytesRead:=SourceStream.Read(DataArray[0],Length(DataArray));
PositionInArray:=FindNullPos(DataArray);
if PositionInArray>-1 then
begin
NullPos:=PositionInFile+PositionInArray;
SourceStream.Seek(NullPos,soBeginning);
repeat
BytesRead:=SourceStream.Read(DataArray[0],Length(DataArray));
BytesWritten:=TargetStream.Write(DataArray[0],BytesRead);
until (BytesRead=0) or (BytesWritten=0);
end;
until (BytesRead=0) or (BytesWritten=0);
finally
SourceStream.Free;
TargetStream.Free;
CloseHandle(SourceFileHandle);
CloseHandle(TargetFileHandle);
Result:=CopyFile(PChar(OutputFile),PChar(CurrentJobFolder+'\video.mkv.lwi'),false);
DeleteFile(OutputFile);
end;
end;