Sample code for 30+ languages & platforms
Zig

SFTP Upload and Download to a BinData Object

See more SFTP Examples

Demonstrates how to SFTP upload from a BinData object, and download into a BinData object.

Chilkat Zig Downloads

Zig
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 bd_a = try chilkat.BinData.init();
    defer bd_a.deinit();
    bd_a.loadFile("qa_data/jpg/penguins.jpg") catch {};

    // Upload the contents of bdA to the SSH/SFTP server.
    const remote_path = "sftpTesting/penguins.jpg";
    sftp.uploadBd(bd_a, remote_path) catch {
        std.debug.print("{s}\n", .{try sftp.getLastErrorText(alloc)});
        return;
    };

    // Download the file..
    const bd_b = try chilkat.BinData.init();
    defer bd_b.deinit();
    sftp.downloadBd(remote_path, bd_b) catch {
        std.debug.print("{s}\n", .{try sftp.getLastErrorText(alloc)});
        return;
    };

    // Verify that bdA and bdB have the exact same contents.
    std.debug.print("size of bdA: {d}\n", .{bd_a.getNumBytes()});
    if (bd_a.contentsEqual(bd_b)) {
        std.debug.print("Contents are equal. Success.\n", .{});
    } else {
        std.debug.print("Contents are NOT equal.  Failed.\n", .{});
    }

    sftp.disconnect();
}