Folgende Funktionen kopieren 2 oder mehrere Dateien aneinander.
Die Dateien werden binär kopiert; vergleichbar mit dem
MS DOS-Befehl
Code:
copy /B fileA + fileB fileA
Delphi-Quellcode:
interface
function ConcatFiles(const DstFile,SrcFile: string; const DeleteDst: Boolean = False): Boolean; overload;
function ConcatFiles(const DstFile :string; const SrcFiles: TStrings; DeleteDst: Boolean = True): Boolean; overload;
implementation
function ConcatFiles(const DstFile,SrcFile: string; const DeleteDst: Boolean = False): Boolean;
const
DefaultBlockSize = 8 * 1024;
var
SrcStream,DstStream: TFileStream;
begin
Result := False; // default: "ConcatFiles failed"
SrcStream := TFileStream.Create(SrcFile,fmOpenRead);
try
if DeleteDst then
DeleteFile(DstFile);
if not FileExists(DstFile) then
begin
DstStream := TFileStream.Create(DstFile,fmCreate);
end else
begin
DstStream := TFileStream.Create(DstFile,fmOpenWrite);
DstStream.Position := DstStream.Size;
end;
while SrcStream.Position < SrcStream.Size do
begin
DstStream.CopyFrom(SrcStream,Min(DefaultBlockSize,SrcStream.Size-SrcStream.Position));
end;
Result := True; // ConcatFiles successfull
except
SrcStream.Free;
DstStream.Free;
end;
end;
function ConcatFiles(const DstFile: string; const SrcFiles: TStrings; DeleteDst: Boolean): Boolean;
var
iFile: Integer;
begin
Result := False;
for iFile := 0 to SrcFiles.Count-1 do
begin
Result := ConcatFiles(DstFile,SrcFiles[iFile],DeleteDst);
DeleteDst := False;
if Result = false then
Break;
end;
end;
[edit=Matze]Code formatiert. Mfg, Matze[/edit]