Sample code for 30+ languages & platforms
Lua

Encrypt Already Existing Zip

See more Zip Examples

To encrypt an already existing non-encrypted .zip, the application must open the .zip, set the encryption related properties, and then re-write.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

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

local zip = chilkat.Zip.new()

-- Open an unencrypted .zip
local success = zip:open_zip("qa_data/zips/test.zip")
if not success then
    print(zip.last_error_text)
    return
end

-- Unzip to a temp directory.
local numFilesUnzipped = zip:unzip("qa_output/tmp")
if numFilesUnzipped < 0 then
    print(zip.last_error_text)
    return
end

-- Clear the zip object.
zip:new_zip("qa_output/aesTest.zip")

-- Indicate that 128-bit AES encryption is to be used when writing the .zip
zip.encryption = 4
zip.encrypt_key_length = 128

-- Set the password.
zip.encrypt_password = "secret"

-- Append the files.
zip.append_from_dir = "qa_output/tmp"
success = zip:append_files("*.*", true)
if not success then
    print(zip.last_error_text)
    return
end

-- Write the .zip and close it.
success = zip:write_zip_and_close()
if not success then
    print(zip.last_error_text)
    return
end

print("Success.")