Zig
Zig
FTP Upload / Download to StringBuilder
Demonstrate how to 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();
// This example assumes Chilkat Ftp2 to have been previously unlocked.
// See Unlock Ftp2 for sample code.
const ftp = try chilkat.Ftp2.init();
defer ftp.deinit();
ftp.setHostname("www.my-ftp-server.com");
ftp.setUsername("mFtpLogin");
ftp.setPassword("myFtpPassword");
// Connect to the FTP server.
ftp.connectOnly() catch {
std.debug.print("{s}\n", .{try ftp.getLastErrorText(alloc)});
return;
};
// Authenticate with the FTP server.
ftp.loginAfterConnectOnly() catch {
std.debug.print("{s}\n", .{try ftp.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 FTP server.
const b_include_bom = false;
const remote_filename = "hamletFromSb.xml";
ftp.putFileSb(sb_a, "utf-8", b_include_bom, remote_filename) catch {
std.debug.print("{s}\n", .{try ftp.getLastErrorText(alloc)});
return;
};
// Download...
const sb_b = try chilkat.StringBuilder.init();
defer sb_b.deinit();
ftp.getFileSb(remote_filename, "utf-8", sb_b) catch {
std.debug.print("{s}\n", .{try ftp.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", .{});
}
ftp.disconnect() catch {};
}