Sample code for 30+ languages & platforms
Tcl

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

Tcl

load ./chilkat.dll

set success 0

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

set zip [new_CkZip]

set success [CkZip_NewZip $zip "qa_output/aes_with_one_unencrypted.zip"]
if {$success == 0} then {
    puts [CkZip_lastErrorText $zip]
    delete_CkZip $zip
    exit
}

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

# A value of 4 indicates WinZip compatible AES encryption.
CkZip_put_Encryption $zip 4

# Key length can be 128, 192, or 256 bits.
CkZip_put_EncryptKeyLength $zip 128

# Set the password for AES encryption:
CkZip_put_EncryptPassword $zip "myPassword"

# Exclude the file helloWorld.txt
# This file will be added unencrypted to the .zip
set saExclusions [new_CkStringArray]

CkStringArray_Append $saExclusions "helloWorld.txt"
CkZip_SetExclusions $zip $saExclusions

CkZip_put_VerboseLogging $zip 1

# Add a directory tree to be zipped.
set recurse 1
# Append from a directory relative to our current working directory.
CkZip_put_AppendFromDir $zip "qa_data/filesToZip"
set success [CkZip_AppendFiles $zip "*" $recurse]
if {$success == 0} then {
    puts [CkZip_lastErrorText $zip]
    delete_CkZip $zip
    delete_CkStringArray $saExclusions
    exit
}

puts [CkZip_lastErrorText $zip]

# Writes qa_output/aes_with_one_unencrypted.zip
set success [CkZip_WriteZipAndClose $zip]
if {$success == 0} then {
    puts [CkZip_lastErrorText $zip]
    delete_CkZip $zip
    delete_CkStringArray $saExclusions
    exit
}

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

set success [CkZip_NewZip $zip "notUsed.zip"]
if {$success == 0} then {
    puts [CkZip_lastErrorText $zip]
    delete_CkZip $zip
    delete_CkStringArray $saExclusions
    exit
}

CkStringArray_Clear $saExclusions
CkZip_SetExclusions $zip $saExclusions

# No encryption.
CkZip_put_Encryption $zip 0

CkZip_put_AppendFromDir $zip "qa_data/filesToZip"

# Add a reference to a file.  This is the file that will
# be added to a already-existing .zip.  
set saveExtraPath 0
set success [CkZip_AddFile $zip "helloWorld.txt" $saveExtraPath]
if {$success == 0} then {
    puts [CkZip_lastErrorText $zip]
    delete_CkZip $zip
    delete_CkStringArray $saExclusions
    exit
}

# 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.
set success [CkZip_QuickAppend $zip "qa_output/aes_with_one_unencrypted.zip"]
if {$success == 0} then {
    puts [CkZip_lastErrorText $zip]
    delete_CkZip $zip
    delete_CkStringArray $saExclusions
    exit
}

puts "Success!"

delete_CkZip $zip
delete_CkStringArray $saExclusions