Zig
Zig
SFTP Upload and Download to a StringBuilder Object
See more SFTP Examples
Demonstrates how to SFTP upload from a Chilkat StringBuilder object, and download into a StringBuilder object.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
var success: bool = false;
// 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();
// Set some timeouts, in milliseconds:
sftp.setConnectTimeoutMs(5000);
sftp.setIdleTimeoutMs(10000);
// Connect to the SSH server and then authenticate.
const port = 22;
success = if (sftp.connect("MY-SSH-SERVER-DOMAIN-OR-IP", port)) true else |_| false;
if (success) {
success = if (sftp.authenticatePw("MY-SSH-LOGIN", "MY-SSH-PASSWORD")) true else |_| false;
if (success) {
success = if (sftp.initializeSftp()) true else |_| false;
}
}
if (!success) {
std.debug.print("{s}\n", .{try sftp.getLastErrorText(alloc)});
return;
}
const sb_a = try chilkat.StringBuilder.init();
defer sb_a.deinit();
sb_a.loadFile("qa_data/hamlet.xml", "utf-8") catch {};
// Upload the contents of sbA to the SSH/SFTP server.
const b_include_bom = false;
const remote_path = "sftpTesting/hamlet.xml";
sftp.uploadSb(sb_a, remote_path, "utf-8", b_include_bom) catch {
std.debug.print("{s}\n", .{try sftp.getLastErrorText(alloc)});
return;
};
// Download the file..
const sb_b = try chilkat.StringBuilder.init();
defer sb_b.deinit();
sftp.downloadSb(remote_path, "utf-8", sb_b) catch {
std.debug.print("{s}\n", .{try sftp.getLastErrorText(alloc)});
return;
};
// Verify that sbA and sbB have the exact same contents.
std.debug.print("size of sbA: {d}\n", .{sb_a.getLength()});
const b_case_sensitive = true;
if (sb_a.contentsEqualSb(sb_b, b_case_sensitive)) {
std.debug.print("Contents are equal. Success.\n", .{});
} else {
std.debug.print("Contents are NOT equal. Failed.\n", .{});
}
sftp.disconnect();
}