Sample code for 30+ languages & platforms
Lua Requires Chilkat v11.0.0+

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 Lua Downloads

Lua
local chilkat = require("chilkat")

-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.

local zip = chilkat.Zip.new()

local success = zip:new_zip("qa_output/aes_with_one_unencrypted.zip")
if not success then
    print(zip.last_error_text)
    return
end

-- Set properties to indicate that the Zip should be
-- AES encrypted.

-- A value of 4 indicates WinZip compatible AES encryption.
zip.encryption = 4

-- Key length can be 128, 192, or 256 bits.
zip.encrypt_key_length = 128

-- Set the password for AES encryption:
zip.encrypt_password = "myPassword"

-- Exclude the file helloWorld.txt
-- This file will be added unencrypted to the .zip
local saExclusions = chilkat.StringArray.new()
saExclusions:append("helloWorld.txt")
zip:set_exclusions(saExclusions)

zip.verbose_logging = true

-- Add a directory tree to be zipped.
local recurse = true
-- Append from a directory relative to our current working directory.
zip.append_from_dir = "qa_data/filesToZip"
success = zip:append_files("*", recurse)
if not success then
    print(zip.last_error_text)
    return
end

print(zip.last_error_text)

-- Writes qa_output/aes_with_one_unencrypted.zip
success = zip:write_zip_and_close()
if not success then
    print(zip.last_error_text)
    return
end

-- ----------------------------------------------
-- 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.

success = zip:new_zip("notUsed.zip")
if not success then
    print(zip.last_error_text)
    return
end

saExclusions:clear()
zip:set_exclusions(saExclusions)

-- No encryption.
zip.encryption = 0

zip.append_from_dir = "qa_data/filesToZip"

-- Add a reference to a file.  This is the file that will
-- be added to a already-existing .zip.
local saveExtraPath = false
success = zip:add_file("helloWorld.txt", saveExtraPath)
if not success then
    print(zip.last_error_text)
    return
end

-- 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.
success = zip:quick_append("qa_output/aes_with_one_unencrypted.zip")
if not success then
    print(zip.last_error_text)
    return
end

print("Success!")