Hab mal spaßenshalber ChatGPT befragt und er hat mir damit geantwortet, schaut schlüssig für mich aus.
Code:
function EncodeFileToBase64(const ASourceFile, ATargetFile: string): Boolean;
var
SourceStream: TFileStream;
TargetStream: TStringStream;
begin
Result := False;
SourceStream := TFileStream.Create(ASourceFile, fmOpenRead or fmShareDenyWrite);
try
TargetStream := TStringStream.Create('', TEncoding.ASCII);
try
TNetEncoding.Base64.Encode(SourceStream, TargetStream);
TargetStream.SaveToFile(ATargetFile);
finally
TargetStream.Free;
end;
finally
SourceStream.Free;
Result := True;
end;
end;
function DecodeFileFromBase64(const ASourceFile, ATargetFile: string): Boolean;
var
SourceStream: TStringStream;
TargetStream: TFileStream;
begin
Result := False;
SourceStream := TStringStream.Create('', TEncoding.ASCII);
try
SourceStream.LoadFromFile(ASourceFile);
TargetStream := TFileStream.Create(ATargetFile, fmCreate);
try
TNetEncoding.Base64.Decode(SourceStream, TargetStream);
finally
TargetStream.Free;
end;
finally
SourceStream.Free;
Result := True;
end;
end;