![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Tcl) Stream a file to compression to encryption to an output file.Runs a chain of streams to read a source file, compress, encrypt, and write an output file.
load ./chilkat.dll # This requires the Chilkat API to have been previously unlocked. # See Global Unlock Sample for sample code. # Initialize the compression object. set compress [new_CkCompression] CkCompression_put_Algorithm $compress "deflate" # Initialize the encryption object. set crypt [new_CkCrypt2] CkCrypt2_put_CryptAlgorithm $crypt "chacha20" CkCrypt2_put_KeyLength $crypt 256 CkCrypt2_put_EncodingMode $crypt "hex" set ivHex "000000000000000000000002" CkCrypt2_SetEncodedIV $crypt $ivHex "hex" CkCrypt2_put_InitialCount $crypt 42 set keyHex "1c9240a5eb55d38af333888604f6b5f0473917c1402b80099dca5cbc207075c0" CkCrypt2_SetEncodedKey $crypt $keyHex "hex" # Setup the streams. # streamC is for compression set streamC [new_CkStream] # streamE is for encryption set streamE [new_CkStream] # The source for the compressor is a file. CkStream_put_SourceFile $streamC "qa_data/hamlet.xml" # The source for the encryptor is the output of the compressor stream. set success [CkStream_SetSourceStream $streamE $streamC] # The sink for the encryptor is the output file. CkStream_put_SinkFile $streamE "qa_output/compressed_encrypted.dat" # Start the compression and encryption streams in background threads.. # taskC is a CkTask set taskC [CkCompression_CompressStreamAsync $compress $streamC] # taskE is a CkTask set taskE [CkCrypt2_EncryptStreamAsync $crypt $streamE] set success [CkTask_Run $taskC] set success [CkTask_Run $taskE] # Let the streams run until finished. # Let's make sure the background task finished. # It should already be the case that the task is finished. while {expr [[CkTask_get_Finished $taskE] != 1] || [[CkTask_get_Finished $taskC] != 1]} { CkTask_SleepMs $taskE 20 } # check for success.. if {[CkTask_get_TaskSuccess $taskE] != 1} then { puts "async encryption failed:" puts [CkTask_resultErrorText $taskE] set success 0 } # check for success.. if {[CkTask_get_TaskSuccess $taskC] != 1} then { puts "async compression failed:" puts [CkTask_resultErrorText $taskC] set success 0 } delete_CkTask $taskE delete_CkTask $taskC # Load the file that was produced and decrypt/decompress to verify. set binData [new_CkBinData] set success [CkBinData_LoadFile $binData "qa_output/compressed_encrypted.dat"] set success [CkCrypt2_DecryptBd $crypt $binData] set success [CkCompression_DecompressBd $compress $binData] # binData should contain the original data.. set success [CkBinData_WriteFile $binData "qa_output/original_hamlet.xml"] puts "Finished." delete_CkCompression $compress delete_CkCrypt2 $crypt delete_CkStream $streamC delete_CkStream $streamE delete_CkBinData $binData |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.