Einzelnen Beitrag anzeigen

Benutzerbild von KodeZwerg
KodeZwerg

Registriert seit: 1. Feb 2018
3.691 Beiträge
 
Delphi 11 Alexandria
 
#3

AW: EncodeBase64 Problem mit Filestream

  Alt 22. Jun 2023, 10:19
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;
Gruß vom KodeZwerg
  Mit Zitat antworten Zitat