Chilkat for Zig — the chilkat package

Chilkat for Zig is a Zig package: one struct per Chilkat class (chilkat.Http, chilkat.JsonObject, …) with init/deinit, error unions for failures, owned strings allocated with the allocator you pass, and progress events delivered to any handler struct you write. HTTP and REST, email (SMTP, POP3, IMAP, MIME), FTP, SFTP, SSH, zip and compression, encryption, digital signatures, certificates, XML, JSON, sockets and more — the same functionality as every other Chilkat product, on Linux, Windows and macOS.

There is nothing to download from this page Add the package with zig fetch --save and build. The Zig package manager downloads the prebuilt Chilkat library for your build target the first time, verifies its hash, and caches it. The offline builds section covers build machines without internet access.

Jump to: Add to your project · Quick start · Supported targets · Offline builds · How the API maps to Zig · Events and threads · Licensing

Documentation & Samples


Add to your project

zig fetch --save https://chilkatdownload.com/11.6.1/chilkat-zig-11.6.1.tar.gz

This downloads the package, computes its hash, and adds it to your build.zig.zon:

.dependencies = .{
    .chilkat = .{
        .url = "https://chilkatdownload.com/11.6.1/chilkat-zig-11.6.1.tar.gz",
        .hash = "chilkat-11.6.1-meH2h_cVRQBe0HsdEAtbobAqUxn2iUOQ0mPseXeWLfOM",
    },
},

Then import the module in build.zig:

const chilkat = b.dependency("chilkat", .{ .target = target, .optimize = optimize });
exe.root_module.addImport("chilkat", chilkat.module("chilkat"));

zig build now links Chilkat. The package declares one lazy dependency per supported target (chilkat-zig-<triple>.tar.gz, a prebuilt static libchilkat.a), so only the archive for the target you are building is ever downloaded, and the Zig package manager checks it against the hash recorded in the package. Cross-compiling is zig build -Dtarget=aarch64-linux-musl — nothing else changes.

Requirements Zig 0.16: 0.16.0 or any later 0.16.x patch release (the package's minimum_zig_version is 0.16.0). Zig 0.17 and later are not supported by this release: Zig changes its build system and standard library with each minor version, so moving to a new Zig version is done in a new Chilkat release. No C or C++ toolchain is needed: the Chilkat library ships prebuilt, and Zig supplies the C and C++ runtime it links against. Each Chilkat release is a new package URL; to upgrade, run zig fetch --save with the new URL.

Quick start

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

pub fn main(init: std.process.Init) !void {
    const alloc = init.gpa;

    // Any non-empty string unlocks the fully functional 30-day trial.
    try chilkat.unlockBundle("Anything for 30-day trial");

    const http = try chilkat.Http.init();
    defer http.deinit();
    http.setConnectTimeout(30);

    const body = http.quickGetStr(alloc, "https://example.com/") catch |err| {
        const text = try http.getLastErrorText(alloc);   // why it failed
        defer alloc.free(text);
        std.debug.print("{s}\n", .{text});
        return err;
    };
    defer alloc.free(body);   // string results are yours to free

    std.debug.print("HTTP {d}, {d} bytes\n", .{ http.getLastStatus(), body.len });
}

Methods that can fail return an error union (error.ChilkatFailed); the object's getLastErrorText explains the failure and the object stays usable. String results are owned copies made with the allocator you pass, and objects returned by a method are owned by you (defer x.deinit()).


Supported targets

A prebuilt Chilkat static library, compiled with zig c++, is published for each of these targets with every Chilkat release. The archive name is chilkat-zig-<triple>.tar.gz.

PlatformTarget (-Dtarget=)Notes
Linux (glibc)x86_64-linux-gnuDebian/Ubuntu, RHEL/Fedora, SUSE, Arch, Raspberry Pi OS, … The library is built against glibc 2.19, so any newer -gnu.X.Y works.
aarch64-linux-gnu
arm-linux-gnueabihf
Linux (musl)x86_64-linux-muslAlpine, and fully static executables on any Linux.
aarch64-linux-musl
Windowsx86_64-windows-gnuZig's default Windows target (mingw-w64, UCRT). windows-msvc is not supported: Zig cannot link a C++ static library for the MSVC ABI without the MSVC toolchain.
aarch64-windows-gnu
macOSaarch64-macosmacOS 11 or later. Building for macOS needs the macOS SDK, so it is done on a Mac (either architecture, from either kind of Mac).
x86_64-macos

The archive is chosen from the build target, not the host. A target not in this table fails the build with a message naming it; chilkat-lib-dir lets you link a library you built yourself, and Chilkat support can tell you about other targets.


Offline builds, vendoring, and your own Chilkat library

To build without downloading the native library, pass a directory containing libchilkat.a for the target as the chilkat-lib-dir option of the dependency:

const chilkat = b.dependency("chilkat", .{
    .target = target,
    .optimize = optimize,
    .@"chilkat-lib-dir" = @as([]const u8, "/opt/chilkat/zig/x86_64-linux-gnu"),
});

With the option set, no native archive is fetched. To populate the directory, download the archive for your target once from a connected machine and unpack it:

https://chilkatdownload.com/<version>/chilkat-zig-<triple>.tar.gz

# for example, for Chilkat 11.6.1 on 64-bit Linux (glibc):
https://chilkatdownload.com/11.6.1/chilkat-zig-x86_64-linux-gnu.tar.gz

Each archive contains libchilkat.a, license.pdf, a software bill of materials and a VERSION file. Alternatively, let a connected machine run the build once and copy the Zig package cache: fetched packages are stored by hash and reused without network access, and the package archive itself can be fetched from a local path or an internal mirror (zig fetch --save /path/to/chilkat-zig-11.6.1.tar.gz) — the hash is the same wherever it comes from.


How the Chilkat API maps to Zig

ChilkatZig
Class Http, JsonObject const http = try chilkat.Http.init(); defer http.deinit(); — a one-pointer struct, passed by value
Property ConnectTimeout http.getConnectTimeout() / http.setConnectTimeout(30)
Method QuickGetStr, S3_UploadString http.quickGetStr(alloc, url), s3UploadString(...) — camelCase
String argument[:0]const u8 (UTF-8): string literals as is; a runtime slice needs try alloc.dupeZ(u8, s)
String resultan owned [:0]u8 allocated with the allocator you pass; defer alloc.free(s)
Method returning success/failure (bool)Error!void: try json.load(text)
Method returning a string or an object (null on failure)an error union: error.ChilkatFailed when Chilkat returns null
Method answering a question (HasMember, IsUnlocked, …)plain bool
LastErrorTexttry obj.getLastErrorText(alloc) — read only when you ask
Object returned by a methodowned by you: const child = try xml.getChild(0); defer child.deinit();
Object argumentpassed by value, filled in by Chilkat, never consumed; optional ones are ?Xml
Byte arrays[]const u8 arguments, owned []u8 results (and BinData)
UnlockBundle (the Global class) try chilkat.unlockBundle(code), once at program start
The C APIchilkat.c.CkHttp.*, bridged by http.handle / Http.fromHandle
*Async methods, TaskNot included in this version; run long calls on a std.Thread.

The reference documentation shows the exact Zig signature of every member; the same text is in the package as doc comments (zig build docs).


Events and threads

The event-capable classes (Http, Ftp2, SFtp, Zip, Bz2, MailMan, …) take any struct that declares one or more of abortCheck, percentDone and progressInfo. The callbacks run on the calling thread, inside the method call; return true to abort.

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

var progress = Progress{};
zip.setEventHandler(&progress);   // progress must outlive the installation
defer zip.clearEventHandler();

A Chilkat object may be used by one thread at a time and may be handed from one thread to another. To cancel from another thread, have abortCheck read a std.atomic.Value(bool).


Licensing

This is the full-version Chilkat product. Chilkat libraries are fully functional for a 30-day evaluation; passing any non-empty string to chilkat.unlockBundle starts the trial, and a purchased unlock code removes the time limit. The license terms are in the LICENSE file of the package (and license.pdf in every native archive). For questions, see the reference documentation or contact Chilkat support.