Sample code for 30+ languages & platforms
Dart Requires Chilkat v11.0.0+

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 Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

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

  final sftp = CkSFtp();

  // Connect to the SSH server.  
  final hostname = 'sftp.example.com';
  final port = 22;
  try {
    sftp.connect(hostname, port);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  try {
    sftp.authenticatePw('myLogin', 'myPassword');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  try {
    sftp.initializeSftp();
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // 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.
  final String handle;
  try {
    handle = sftp.openDir('/+mode=binary');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

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

  final dirListing = CkSFtpDir();
  try {
    sftp.readDirListing(handle, dirListing);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Close the directory handle:
  try {
    sftp.closeHandle(handle);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Download the binary file:
  final localFilePath = 'c:/temp/test.zip';
  final remoteFilePath = 'test.zip';
  try {
    sftp.downloadFileByName(remoteFilePath, localFilePath);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  print('Success.');
}