PureBasic
PureBasic
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 PureBasic Downloads
IncludeFile "CkZip.pb"
IncludeFile "CkStringArray.pb"
Procedure ChilkatExample()
success.i = 0
; This example requires the Chilkat API to have been previously unlocked.
; See Global Unlock Sample for sample code.
zip.i = CkZip::ckCreate()
If zip.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkZip::ckNewZip(zip,"qa_output/aes_with_one_unencrypted.zip")
If success = 0
Debug CkZip::ckLastErrorText(zip)
CkZip::ckDispose(zip)
ProcedureReturn
EndIf
; Set properties to indicate that the Zip should be
; AES encrypted.
; A value of 4 indicates WinZip compatible AES encryption.
CkZip::setCkEncryption(zip, 4)
; Key length can be 128, 192, or 256 bits.
CkZip::setCkEncryptKeyLength(zip, 128)
; Set the password for AES encryption:
CkZip::setCkEncryptPassword(zip, "myPassword")
; Exclude the file helloWorld.txt
; This file will be added unencrypted to the .zip
saExclusions.i = CkStringArray::ckCreate()
If saExclusions.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkStringArray::ckAppend(saExclusions,"helloWorld.txt")
CkZip::ckSetExclusions(zip,saExclusions)
CkZip::setCkVerboseLogging(zip, 1)
; Add a directory tree to be zipped.
recurse.i = 1
; Append from a directory relative to our current working directory.
CkZip::setCkAppendFromDir(zip, "qa_data/filesToZip")
success = CkZip::ckAppendFiles(zip,"*",recurse)
If success = 0
Debug CkZip::ckLastErrorText(zip)
CkZip::ckDispose(zip)
CkStringArray::ckDispose(saExclusions)
ProcedureReturn
EndIf
Debug CkZip::ckLastErrorText(zip)
; Writes qa_output/aes_with_one_unencrypted.zip
success = CkZip::ckWriteZipAndClose(zip)
If success = 0
Debug CkZip::ckLastErrorText(zip)
CkZip::ckDispose(zip)
CkStringArray::ckDispose(saExclusions)
ProcedureReturn
EndIf
; ----------------------------------------------
; 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.
success = CkZip::ckNewZip(zip,"notUsed.zip")
If success = 0
Debug CkZip::ckLastErrorText(zip)
CkZip::ckDispose(zip)
CkStringArray::ckDispose(saExclusions)
ProcedureReturn
EndIf
CkStringArray::ckClear(saExclusions)
CkZip::ckSetExclusions(zip,saExclusions)
; No encryption.
CkZip::setCkEncryption(zip, 0)
CkZip::setCkAppendFromDir(zip, "qa_data/filesToZip")
; Add a reference to a file. This is the file that will
; be added to a already-existing .zip.
saveExtraPath.i = 0
success = CkZip::ckAddFile(zip,"helloWorld.txt",saveExtraPath)
If success = 0
Debug CkZip::ckLastErrorText(zip)
CkZip::ckDispose(zip)
CkStringArray::ckDispose(saExclusions)
ProcedureReturn
EndIf
; 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.
success = CkZip::ckQuickAppend(zip,"qa_output/aes_with_one_unencrypted.zip")
If success = 0
Debug CkZip::ckLastErrorText(zip)
CkZip::ckDispose(zip)
CkStringArray::ckDispose(saExclusions)
ProcedureReturn
EndIf
Debug "Success!"
CkZip::ckDispose(zip)
CkStringArray::ckDispose(saExclusions)
ProcedureReturn
EndProcedure