![]() |
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
(PureBasic) 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.
IncludeFile "CkTask.pb" IncludeFile "CkBinData.pb" IncludeFile "CkCompression.pb" IncludeFile "CkStream.pb" IncludeFile "CkCrypt2.pb" Procedure ChilkatExample() ; This requires the Chilkat API to have been previously unlocked. ; See Global Unlock Sample for sample code. success.i ; Initialize the compression object. compress.i = CkCompression::ckCreate() If compress.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkCompression::setCkAlgorithm(compress, "deflate") ; Initialize the encryption object. crypt.i = CkCrypt2::ckCreate() If crypt.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkCrypt2::setCkCryptAlgorithm(crypt, "chacha20") CkCrypt2::setCkKeyLength(crypt, 256) CkCrypt2::setCkEncodingMode(crypt, "hex") ivHex.s = "000000000000000000000002" CkCrypt2::ckSetEncodedIV(crypt,ivHex,"hex") CkCrypt2::setCkInitialCount(crypt, 42) keyHex.s = "1c9240a5eb55d38af333888604f6b5f0473917c1402b80099dca5cbc207075c0" CkCrypt2::ckSetEncodedKey(crypt,keyHex,"hex") ; Setup the streams. ; streamC is for compression streamC.i = CkStream::ckCreate() If streamC.i = 0 Debug "Failed to create object." ProcedureReturn EndIf ; streamE is for encryption streamE.i = CkStream::ckCreate() If streamE.i = 0 Debug "Failed to create object." ProcedureReturn EndIf ; The source for the compressor is a file. CkStream::setCkSourceFile(streamC, "qa_data/hamlet.xml") ; The source for the encryptor is the output of the compressor stream. success = CkStream::ckSetSourceStream(streamE,streamC) ; The sink for the encryptor is the output file. CkStream::setCkSinkFile(streamE, "qa_output/compressed_encrypted.dat") ; Start the compression and encryption streams in background threads.. taskC.i = CkCompression::ckCompressStreamAsync(compress,streamC) taskE.i = CkCrypt2::ckEncryptStreamAsync(crypt,streamE) success = CkTask::ckRun(taskC) success = CkTask::ckRun(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 ((CkTask::ckFinished(taskE) <> 1) OR (CkTask::ckFinished(taskC) <> 1)) CkTask::ckSleepMs(taskE,20) Wend ; check for success.. If CkTask::ckTaskSuccess(taskE) <> 1 Debug "async encryption failed:" Debug CkTask::ckResultErrorText(taskE) success = 0 EndIf ; check for success.. If CkTask::ckTaskSuccess(taskC) <> 1 Debug "async compression failed:" Debug CkTask::ckResultErrorText(taskC) success = 0 EndIf CkTask::ckDispose(taskE) CkTask::ckDispose(taskC) ; Load the file that was produced and decrypt/decompress to verify. binData.i = CkBinData::ckCreate() If binData.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success = CkBinData::ckLoadFile(binData,"qa_output/compressed_encrypted.dat") success = CkCrypt2::ckDecryptBd(crypt,binData) success = CkCompression::ckDecompressBd(compress,binData) ; binData should contain the original data.. success = CkBinData::ckWriteFile(binData,"qa_output/original_hamlet.xml") Debug "Finished." CkCompression::ckDispose(compress) CkCrypt2::ckDispose(crypt) CkStream::ckDispose(streamC) CkStream::ckDispose(streamE) CkBinData::ckDispose(binData) ProcedureReturn EndProcedure |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.