Sample code for 30+ languages & platforms
Zig

FTP Download with Progress Event Callbacks

See more FTP Examples

FTP download with progress event callbacks.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

const FtpEvents = struct {
    pub fn percentDone(_: *FtpEvents, pct: i32) bool {
        std.debug.print("Percent Done: {d}\n", .{pct});
        return false; // return true to abort the method in progress
    }

    pub fn progressInfo(_: *FtpEvents, name: [:0]const u8, value: [:0]const u8) void {
        std.debug.print("{s}: {s}\n", .{ name, value });
    }
};

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 ftp = try chilkat.Ftp2.init();
    defer ftp.deinit();
    var ftp_events = FtpEvents{};
    ftp.setEventHandler(&ftp_events);

    ftp.setHostname("ftp.someFtpServer.com");
    ftp.setUsername("myLogin");
    ftp.setPassword("myPassword");

    // Connect and login to the FTP server.
    ftp.connectOnly() catch {
        std.debug.print("{s}\n", .{try ftp.getLastErrorText(alloc)});
        return;
    };

    ftp.loginAfterConnectOnly() catch {
        std.debug.print("{s}\n", .{try ftp.getLastErrorText(alloc)});
        return;
    };

    // Change to the remote directory where the file is located.
    // This step is only necessary if the file is not in the home directory
    // of the FTP account.
    ftp.changeRemoteDir("junk") catch {
        std.debug.print("{s}\n", .{try ftp.getLastErrorText(alloc)});
        return;
    };

    const local_filename = "c:/temp/hamlet.xml";
    const remote_filename = "hamlet.xml";

    // Ensure that we get PercentDone callbacks.
    ftp.setAutoGetSizeForProgress(true);

    // Download the file.
    ftp.getFile(remote_filename, local_filename) catch {
        std.debug.print("{s}\n", .{try ftp.getLastErrorText(alloc)});
        return;
    };

    try ftp.disconnect();

    std.debug.print("File Downloaded!\n", .{});
}