Sample code for 30+ languages & platforms
Zig

SFTP Read Text File to String

See more SFTP Examples

Demonstrates how to download a text file from an SSH server directly into a string variable.

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();

    // This example assumes 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(15000);

    // Connect to the SSH server.
    // The standard SSH port = 22
    // The hostname may be a hostname or IP address.
    const hostname = "sftp.example.com";
    const port = 22;
    sftp.connect(hostname, port) catch {
        std.debug.print("{s}\n", .{try sftp.getLastErrorText(alloc)});
        return;
    };

    // Authenticate with the SSH server.  Chilkat SFTP supports
    // both password-based authenication as well as public-key
    // authentication.  This example uses password authenication.
    sftp.authenticatePw("myLogin", "myPassword") catch {
        std.debug.print("{s}\n", .{try sftp.getLastErrorText(alloc)});
        return;
    };

    // After authenticating, the SFTP subsystem must be initialized:
    sftp.initializeSftp() catch {
        std.debug.print("{s}\n", .{try sftp.getLastErrorText(alloc)});
        return;
    };

    // Open a file on the server:
    const handle = sftp.openFile(alloc, "hamlet.xml", "readOnly", "openExisting") catch {
        std.debug.print("{s}\n", .{try sftp.getLastErrorText(alloc)});
        return;
    };

    // Get the total size of this file (in bytes)
    const b_follow_links = false;
    const b_is_handle = true;

    // bFollowLinks is ignored because we are passing a handle
    // and not a remote filename.
    // There are alternative methods for handling file sizes
    // greater than 32-bit.  (See the reference documentation.)
    const num_bytes = sftp.getFileSize32(handle, b_follow_links, b_is_handle);
    if (num_bytes < 0) {
        std.debug.print("{s}\n", .{try sftp.getLastErrorText(alloc)});
        return;
    }

    // The charset indicates the character encoding of the text
    // file on the SSH server.   Setting the charset correctly
    // allows the Chilkat SFTP component to correctly interpret
    // the bytes that represent the characters.
    const charset = "ansi";
    const file_contents = sftp.readFileText(alloc, handle, num_bytes, charset) catch "";
    if (!sftp.getLastMethodSuccess()) {
        std.debug.print("{s}\n", .{try sftp.getLastErrorText(alloc)});
        return;
    } else {
        std.debug.print("Received file:\n", .{});
        std.debug.print("{s}\n", .{file_contents});
    }

    // Close the file.
    sftp.closeHandle(handle) catch {
        std.debug.print("{s}\n", .{try sftp.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Success.\n", .{});
}