Sample code for 30+ languages & platforms
DataFlex

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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoZip
    Integer iNumFilesUnzipped
    String sTemp1

    Move False To iSuccess

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

    Get Create (RefClass(cComChilkatZip)) To hoZip
    If (Not(IsComObjectCreated(hoZip))) Begin
        Send CreateComObject of hoZip
    End

    // Open an unencrypted .zip
    Get ComOpenZip Of hoZip "qa_data/zips/test.zip" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoZip To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Unzip to a temp directory.
    Get ComUnzip Of hoZip "qa_output/tmp" To iNumFilesUnzipped
    If (iNumFilesUnzipped < 0) Begin
        Get ComLastErrorText Of hoZip To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Clear the zip object.
    Get ComNewZip Of hoZip "qa_output/aesTest.zip" To iSuccess

    // Indicate that 128-bit AES encryption is to be used when writing the .zip
    Set ComEncryption Of hoZip To 4
    Set ComEncryptKeyLength Of hoZip To 128

    // Set the password.
    Set ComEncryptPassword Of hoZip To "secret"

    // Append the files.
    Set ComAppendFromDir Of hoZip To "qa_output/tmp"
    Get ComAppendFiles Of hoZip "*.*" True To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoZip To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Write the .zip and close it.
    Get ComWriteZipAndClose Of hoZip To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoZip To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Success."


End_Procedure