Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Co:Z SFTP Binary File Download (from z/OS IBM Mainframe)

See more SFTP Examples

Demonstrates how to download a binary file, such as a .zip, from a Co:Z SFTP server on a z/OS IBM Mainframe.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.SFtp,
  Chilkat.SFtpDir;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  sftp: TSFtp;
  hostname: string;
  port: Integer;
  handle: string;
  dirListing: TSFtpDir;
  localFilePath: string;
  remoteFilePath: string;

begin
  success := False;

  //  This example requires the Chilkat API to have been previously unlocked.
  //  See Global Unlock Sample for sample code.

  sftp := TSFtp.Create;

  //  Connect to the SSH server.  
  hostname := 'sftp.example.com';
  port := 22;
  success := sftp.Connect(hostname,port);
  if (success = False) then
    begin
      WriteLn(sftp.LastErrorText);
      Exit;
    end;

  success := sftp.AuthenticatePw('myLogin','myPassword');
  if (success = False) then
    begin
      WriteLn(sftp.LastErrorText);
      Exit;
    end;

  success := sftp.InitializeSftp();
  if (success = False) then
    begin
      WriteLn(sftp.LastErrorText);
      Exit;
    end;

  //  To download a binary file from the Co:Z SFTP server, 
  //  we must switch to binary mode in the following unconventional way.
  //  We pretend to fetch a directory listing for "/+mode=binary"
  //  This has the effect of putting the server in binary mode for transfers.
  handle := sftp.OpenDir('/+mode=binary');
  if (sftp.LastMethodSuccess = False) then
    begin
      WriteLn(sftp.LastErrorText);
      Exit;
    end;

  //  Download the "directory listing" (but it's not actually a directory listing, and we'll just discard it.)

  dirListing := TSFtpDir.Create;
  success := sftp.ReadDirListing(handle,dirListing);
  if (success = False) then
    begin
      WriteLn(sftp.LastErrorText);
      Exit;
    end;

  //  Close the directory handle:
  success := sftp.CloseHandle(handle);
  if (success = False) then
    begin
      WriteLn(sftp.LastErrorText);
      Exit;
    end;

  //  Download the binary file:
  localFilePath := 'c:/temp/test.zip';
  remoteFilePath := 'test.zip';
  success := sftp.DownloadFileByName(remoteFilePath,localFilePath);
  if (success = False) then
    begin
      WriteLn(sftp.LastErrorText);
      Exit;
    end;

  WriteLn('Success.');


  sftp.Free;
  dirListing.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.