Zig
Zig
Download a Zip from a URL and OpenBd. (No .zip file is created)
See more Zip Examples
Demonstrates how to download a .zip from a URL, opens the Zip, and gets the contents of a file. No file is ever written.Chilkat Zig Downloads
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 http = try chilkat.Http.init();
defer http.deinit();
const bd = try chilkat.BinData.init();
defer bd.deinit();
// This URL is valid and can be tested...
success = if (http.quickGetBd("https://chilkatdownload.com/example_data/hamlet.zip", bd)) true else |_| false;
if (!http.getLastMethodSuccess()) {
std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
return;
}
const zip = try chilkat.Zip.init();
defer zip.deinit();
// Open the zip from the bytes contained in bd.
success = if (zip.openBd(bd)) true else |_| false;
if (!success) {
std.debug.print("{s}\n", .{try zip.getLastErrorText(alloc)});
return;
}
// Get the entry for the file we want..
const entry = try chilkat.ZipEntry.init();
defer entry.deinit();
success = zip.entryOf("hamlet.xml", entry);
if (!success) {
std.debug.print("{s}\n", .{try zip.getLastErrorText(alloc)});
return;
}
// Convert all line endings to CRLF (if needed)
const line_ending_behavior = 2;
const xml_str = entry.unzipToString(alloc, line_ending_behavior, "utf-8") catch {
std.debug.print("{s}\n", .{try entry.getLastErrorText(alloc)});
return;
};
// The XML in this case is about 274K, so let's just examine the last 20 lines...
const sb = try chilkat.StringBuilder.init();
defer sb.deinit();
sb.append(xml_str) catch {};
std.debug.print("{s}\n", .{try sb.lastNLines(alloc, 20, true)});
}