Sample code for 30+ languages & platforms
DataFlex

Remove an Entry from an Existing ZIP Using DeleteEntry

See more Zip Examples

This example demonstrates how to use the DeleteEntry method to remove a file from an existing ZIP archive.

The example:

  • Creates a ZIP archive containing three text files
  • Opens the ZIP archive for modification
  • Finds and deletes one entry
  • Writes the modified ZIP archive to a new filename

Suppose the original ZIP archive contains:

a.txt
b.txt
c.txt

After deleting b.txt, the modified ZIP archive contains:

a.txt
c.txt

The entry is removed only from the in-memory ZIP object until a Write* method is called.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoZip
    String sCharset
    Handle hoZip2
    Variant vEntry
    Handle hoEntry
    String sTemp1

    Move False To iSuccess

    Move False To iSuccess

    // ------------------------------------------------------------
    // First create a ZIP archive containing three text files.

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

    Get ComNewZip Of hoZip "original.zip" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoZip To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Move "utf-8" To sCharset

    Get ComAddString Of hoZip "a.txt" "Contents of file A" sCharset To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoZip To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComAddString Of hoZip "b.txt" "Contents of file B" sCharset To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoZip To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComAddString Of hoZip "c.txt" "Contents of file C" sCharset To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoZip To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Write the ZIP archive to disk.
    // 
    // The ZIP now contains:
    // 
    //     a.txt
    //     b.txt
    //     c.txt
    // 
    Get ComWriteZipAndClose Of hoZip To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoZip To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // ------------------------------------------------------------
    // Open the existing ZIP archive for modification.

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

    Get ComOpenZip Of hoZip2 "original.zip" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoZip2 To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Find the entry named "b.txt".
    Get Create (RefClass(cComChilkatZipEntry)) To hoEntry
    If (Not(IsComObjectCreated(hoEntry))) Begin
        Send CreateComObject of hoEntry
    End

    Get pvComObject of hoEntry to vEntry
    Get ComEntryOf Of hoZip2 "b.txt" vEntry To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoZip2 To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Remove the entry from the in-memory ZIP object.
    // 
    // At this point, the original ZIP file on disk is unchanged.
    // The deletion takes effect only after WriteZip or
    // WriteZipAndClose is called.
    Get pvComObject of hoEntry to vEntry
    Get ComDeleteEntry Of hoZip2 vEntry To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoZip2 To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Write the modified ZIP archive to a new file.
    Set ComFileName Of hoZip2 To "modified.zip"

    Get ComWriteZipAndClose Of hoZip2 To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoZip2 To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // The modified ZIP now contains:
    // 
    //     a.txt
    //     c.txt
    // 

    Showln "ZIP archive updated successfully."


End_Procedure