Sample code for 30+ languages & platforms
Xbase++

Encrypt Already Existing Zip

See more Zip Examples

To encrypt an already existing non-encrypted .zip, the application must open the .zip, set the encryption related properties, and then re-write.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oZip
LOCAL nNumFilesUnzipped

nSuccess := 0

//  This requires the Chilkat Zip API to have been previously unlocked.
//  See Unlock Chilkat Zip for sample code.

oZip := CreateObject("Chilkat.Zip")

//  Open an unencrypted .zip
nSuccess := oZip:OpenZip("qa_data/zips/test.zip")
IF (nSuccess != 1)
    ? oZip:LastErrorText
    oZip:destroy()
    RETURN
ENDIF

//  Unzip to a temp directory.
nNumFilesUnzipped := oZip:Unzip("qa_output/tmp")
IF (nNumFilesUnzipped < 0)
    ? oZip:LastErrorText
    oZip:destroy()
    RETURN
ENDIF

//  Clear the zip object.
oZip:NewZip("qa_output/aesTest.zip")

//  Indicate that 128-bit AES encryption is to be used when writing the .zip
oZip:Encryption := 4
oZip:EncryptKeyLength := 128

//  Set the password.
oZip:EncryptPassword := "secret"

//  Append the files.
oZip:AppendFromDir := "qa_output/tmp"
nSuccess := oZip:AppendFiles("*.*", 1)
IF (nSuccess != 1)
    ? oZip:LastErrorText
    oZip:destroy()
    RETURN
ENDIF

//  Write the .zip and close it.
nSuccess := oZip:WriteZipAndClose()
IF (nSuccess != 1)
    ? oZip:LastErrorText
    oZip:destroy()
    RETURN
ENDIF

? "Success."

oZip:destroy()