Rust Requires Chilkat v11.0.0+
Rust
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 Rust Downloads
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let sftp = chilkat::SFtp::new();
// Connect to the SSH server.
let hostname = "sftp.example.com".to_string();
let port = 22;
if sftp.connect(&hostname, port).is_err() {
println!("{}", sftp.last_error_text());
return;
}
if sftp.authenticate_pw("myLogin", "myPassword").is_err() {
println!("{}", sftp.last_error_text());
return;
}
if sftp.initialize_sftp().is_err() {
println!("{}", sftp.last_error_text());
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.
let Ok(handle) = sftp.open_dir("/+mode=binary") else {
println!("{}", sftp.last_error_text());
return;
};
// Download the "directory listing" (but it's not actually a directory listing, and we'll just discard it.)
let dir_listing = chilkat::SFtpDir::new();
if sftp.read_dir_listing(&handle, &dir_listing).is_err() {
println!("{}", sftp.last_error_text());
return;
}
// Close the directory handle:
if sftp.close_handle(&handle).is_err() {
println!("{}", sftp.last_error_text());
return;
}
// Download the binary file:
let local_file_path = "c:/temp/test.zip".to_string();
let remote_file_path = "test.zip".to_string();
if sftp.download_file_by_name(&remote_file_path, &local_file_path).is_err() {
println!("{}", sftp.last_error_text());
return;
}
println!("Success.");