Zig Requires Chilkat v11.0.0+
Zig
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 Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const sftp = try chilkat.SFtp.init();
defer sftp.deinit();
// Connect to the SSH server.
const hostname = "sftp.example.com";
const port = 22;
sftp.connect(hostname, port) catch {
std.debug.print("{s}\n", .{try sftp.getLastErrorText(alloc)});
return;
};
sftp.authenticatePw("myLogin", "myPassword") catch {
std.debug.print("{s}\n", .{try sftp.getLastErrorText(alloc)});
return;
};
sftp.initializeSftp() catch {
std.debug.print("{s}\n", .{try sftp.getLastErrorText(alloc)});
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.
const handle = sftp.openDir(alloc, "/+mode=binary") catch {
std.debug.print("{s}\n", .{try sftp.getLastErrorText(alloc)});
return;
};
// Download the "directory listing" (but it's not actually a directory listing, and we'll just discard it.)
const dir_listing = try chilkat.SFtpDir.init();
defer dir_listing.deinit();
sftp.readDirListing(handle, dir_listing) catch {
std.debug.print("{s}\n", .{try sftp.getLastErrorText(alloc)});
return;
};
// Close the directory handle:
sftp.closeHandle(handle) catch {
std.debug.print("{s}\n", .{try sftp.getLastErrorText(alloc)});
return;
};
// Download the binary file:
const local_file_path = "c:/temp/test.zip";
const remote_file_path = "test.zip";
sftp.downloadFileByName(remote_file_path, local_file_path) catch {
std.debug.print("{s}\n", .{try sftp.getLastErrorText(alloc)});
return;
};
std.debug.print("Success.\n", .{});
}