Tcl
Tcl
Compress and Encrypt a Large File (Low and Constant Memory Footprint)
See more Compression Examples
Demonstrates how to compress and encrypt a large file such that the memory footprint remains low and constant.Note: This example requires Chilkat v9.5.0.99 or greater.
Chilkat Tcl Downloads
load ./chilkat.dll
set success 0
# This example assumes the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.
set compress [new_CkCompression]
CkCompression_put_Algorithm $compress "deflate"
# Set encryption params.
# The possible values are the same as for the corresponding properties in the Chilkat Crypt2 class/object.
# The encoded IV and Key must be specified as hex.
set json [new_CkJsonObject]
CkJsonObject_UpdateString $json "cryptAlgorithm" "aes"
CkJsonObject_UpdateString $json "cipherMode" "cbc"
CkJsonObject_UpdateInt $json "keyLength" 128
CkJsonObject_UpdateInt $json "paddingScheme" 0
CkJsonObject_UpdateString $json "encodedIV" "000102030405060708090A0B0C0D0E0F"
CkJsonObject_UpdateString $json "encodedKey" "000102030405060708090A0B0C0D0E0F"
# Do file-to-file compression+encryption in a single call.
set inPath "qa_data/largeFile.dat"
set outPath "c:/temp/qa_output/compressed_encrypted.dat"
set success [CkCompression_CompressEncryptFile $compress $json $inPath $outPath]
if {$success == 0} then {
puts [CkCompression_lastErrorText $compress]
delete_CkCompression $compress
delete_CkJsonObject $json
exit
}
# We can do file-to-file decrypt/decompress like this:
set inPath2 $outPath
set outPath2 "c:/temp/qa_output/restored.dat"
set success [CkCompression_DecryptDecompressFile $compress $json $inPath2 $outPath2]
if {$success == 0} then {
puts [CkCompression_lastErrorText $compress]
delete_CkCompression $compress
delete_CkJsonObject $json
exit
}
# Note: The above decrypt + decompress is the equivalent of doing the same in these two steps:
set crypt [new_CkCrypt2]
CkCrypt2_put_CryptAlgorithm $crypt "aes"
CkCrypt2_put_CipherMode $crypt "cbc"
CkCrypt2_put_KeyLength $crypt 128
CkCrypt2_put_PaddingScheme $crypt 0
CkCrypt2_SetEncodedIV $crypt "000102030405060708090A0B0C0D0E0F" "hex"
CkCrypt2_SetEncodedKey $crypt "000102030405060708090A0B0C0D0E0F" "hex"
set decryptedPath "c:/temp/qa_output/decrypted.dat"
set success [CkCrypt2_CkDecryptFile $crypt $inPath2 $decryptedPath]
if {$success == 0} then {
puts [CkCrypt2_lastErrorText $crypt]
delete_CkCompression $compress
delete_CkJsonObject $json
delete_CkCrypt2 $crypt
exit
}
set outPath3 "c:/temp/qa_output/restored_in_two_steps.dat"
set success [CkCompression_DecompressFile $compress $decryptedPath $outPath3]
if {$success == 0} then {
puts [CkCompression_lastErrorText $compress]
delete_CkCompression $compress
delete_CkJsonObject $json
delete_CkCrypt2 $crypt
exit
}
puts "Success."
delete_CkCompression $compress
delete_CkJsonObject $json
delete_CkCrypt2 $crypt