Zig Requires Chilkat v11.0.0+
Zig
Creates an AES Encrypted Zip with One File Unencrypted
See more Zip Examples
Demonstrates how to create an AES encrypted zip, but also containing one file that is not encrypted. The way we do it is to first create an AES encrypted zip in the usual way, and then we append an unecrypted file to it.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
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 zip = try chilkat.Zip.init();
defer zip.deinit();
zip.newZip("qa_output/aes_with_one_unencrypted.zip") catch {
std.debug.print("{s}\n", .{try zip.getLastErrorText(alloc)});
return;
};
// Set properties to indicate that the Zip should be
// AES encrypted.
// A value of 4 indicates WinZip compatible AES encryption.
zip.setEncryption(4);
// Key length can be 128, 192, or 256 bits.
zip.setEncryptKeyLength(128);
// Set the password for AES encryption:
zip.setEncryptPassword("myPassword");
// Exclude the file helloWorld.txt
// This file will be added unencrypted to the .zip
const sa_exclusions = try chilkat.StringArray.init();
defer sa_exclusions.deinit();
sa_exclusions.append("helloWorld.txt") catch {};
zip.setExclusions(sa_exclusions);
zip.setVerboseLogging(true);
// Add a directory tree to be zipped.
const recurse = true;
// Append from a directory relative to our current working directory.
zip.setAppendFromDir("qa_data/filesToZip");
zip.appendFiles("*", recurse) catch {
std.debug.print("{s}\n", .{try zip.getLastErrorText(alloc)});
return;
};
std.debug.print("{s}\n", .{try zip.getLastErrorText(alloc)});
// Writes qa_output/aes_with_one_unencrypted.zip
zip.writeZipAndClose() catch {
std.debug.print("{s}\n", .{try zip.getLastErrorText(alloc)});
return;
};
// ----------------------------------------------
// At this point, we have an encrypted .zip with all
// files except for helloWorld.txt.
// We'll add helloWorld.txt (unencrypted) to the .zip we just created.
// The NewZip method only initializes the Zip object -- it does
// not create or write a .zip file.
zip.newZip("notUsed.zip") catch {
std.debug.print("{s}\n", .{try zip.getLastErrorText(alloc)});
return;
};
sa_exclusions.clear();
zip.setExclusions(sa_exclusions);
// No encryption.
zip.setEncryption(0);
zip.setAppendFromDir("qa_data/filesToZip");
// Add a reference to a file. This is the file that will
// be added to a already-existing .zip.
const save_extra_path = false;
zip.addFile("helloWorld.txt", save_extra_path) catch {
std.debug.print("{s}\n", .{try zip.getLastErrorText(alloc)});
return;
};
// Appends the contents of the zip object to the preExisting.zip
// zip archive. preExisting.zip is opened, and the files
// referenced by this zip object are streamed in, compressed,
// and appended to the end of the archive.
zip.quickAppend("qa_output/aes_with_one_unencrypted.zip") catch {
std.debug.print("{s}\n", .{try zip.getLastErrorText(alloc)});
return;
};
std.debug.print("Success!\n", .{});
}