Sample code for 30+ languages & platforms
PureBasic

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

PureBasic
IncludeFile "CkZip.pb"
IncludeFile "CkZipEntry.pb"

Procedure ChilkatExample()

    success.i = 0

    success = 0

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

    zip.i = CkZip::ckCreate()
    If zip.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkZip::ckNewZip(zip,"original.zip")
    If success = 0
        Debug CkZip::ckLastErrorText(zip)
        CkZip::ckDispose(zip)
        ProcedureReturn
    EndIf

    charset.s = "utf-8"

    success = CkZip::ckAddString(zip,"a.txt","Contents of file A",charset)
    If success = 0
        Debug CkZip::ckLastErrorText(zip)
        CkZip::ckDispose(zip)
        ProcedureReturn
    EndIf

    success = CkZip::ckAddString(zip,"b.txt","Contents of file B",charset)
    If success = 0
        Debug CkZip::ckLastErrorText(zip)
        CkZip::ckDispose(zip)
        ProcedureReturn
    EndIf

    success = CkZip::ckAddString(zip,"c.txt","Contents of file C",charset)
    If success = 0
        Debug CkZip::ckLastErrorText(zip)
        CkZip::ckDispose(zip)
        ProcedureReturn
    EndIf

    ; Write the ZIP archive to disk.
    ; 
    ; The ZIP now contains:
    ; 
    ;     a.txt
    ;     b.txt
    ;     c.txt
    ; 
    success = CkZip::ckWriteZipAndClose(zip)
    If success = 0
        Debug CkZip::ckLastErrorText(zip)
        CkZip::ckDispose(zip)
        ProcedureReturn
    EndIf

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

    zip2.i = CkZip::ckCreate()
    If zip2.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkZip::ckOpenZip(zip2,"original.zip")
    If success = 0
        Debug CkZip::ckLastErrorText(zip2)
        CkZip::ckDispose(zip)
        CkZip::ckDispose(zip2)
        ProcedureReturn
    EndIf

    ; Find the entry named "b.txt".
    entry.i = CkZipEntry::ckCreate()
    If entry.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkZip::ckEntryOf(zip2,"b.txt",entry)
    If success = 0
        Debug CkZip::ckLastErrorText(zip2)
        CkZip::ckDispose(zip)
        CkZip::ckDispose(zip2)
        CkZipEntry::ckDispose(entry)
        ProcedureReturn
    EndIf

    ; 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.
    success = CkZip::ckDeleteEntry(zip2,entry)
    If success = 0
        Debug CkZip::ckLastErrorText(zip2)
        CkZip::ckDispose(zip)
        CkZip::ckDispose(zip2)
        CkZipEntry::ckDispose(entry)
        ProcedureReturn
    EndIf

    ; Write the modified ZIP archive to a new file.
    CkZip::setCkFileName(zip2, "modified.zip")

    success = CkZip::ckWriteZipAndClose(zip2)
    If success = 0
        Debug CkZip::ckLastErrorText(zip2)
        CkZip::ckDispose(zip)
        CkZip::ckDispose(zip2)
        CkZipEntry::ckDispose(entry)
        ProcedureReturn
    EndIf

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

    Debug "ZIP archive updated successfully."


    CkZip::ckDispose(zip)
    CkZip::ckDispose(zip2)
    CkZipEntry::ckDispose(entry)


    ProcedureReturn
EndProcedure