Sample code for 30+ languages & platforms
Xbase++ Requires Chilkat v11.0.0+

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oZip
LOCAL cCharset
LOCAL oZip2
LOCAL oEntry

nSuccess := 0

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

oZip := CreateObject("Chilkat.Zip")

nSuccess := oZip:NewZip("original.zip")
IF (nSuccess == 0)
    ? oZip:LastErrorText
    oZip:destroy()
    RETURN
ENDIF

cCharset := "utf-8"

nSuccess := oZip:AddString("a.txt", "Contents of file A", cCharset)
IF (nSuccess == 0)
    ? oZip:LastErrorText
    oZip:destroy()
    RETURN
ENDIF

nSuccess := oZip:AddString("b.txt", "Contents of file B", cCharset)
IF (nSuccess == 0)
    ? oZip:LastErrorText
    oZip:destroy()
    RETURN
ENDIF

nSuccess := oZip:AddString("c.txt", "Contents of file C", cCharset)
IF (nSuccess == 0)
    ? oZip:LastErrorText
    oZip:destroy()
    RETURN
ENDIF

//  Write the ZIP archive to disk.
//  
//  The ZIP now contains:
//  
//      a.txt
//      b.txt
//      c.txt
//  
nSuccess := oZip:WriteZipAndClose()
IF (nSuccess == 0)
    ? oZip:LastErrorText
    oZip:destroy()
    RETURN
ENDIF

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

oZip2 := CreateObject("Chilkat.Zip")

nSuccess := oZip2:OpenZip("original.zip")
IF (nSuccess == 0)
    ? oZip2:LastErrorText
    oZip:destroy()
    oZip2:destroy()
    RETURN
ENDIF

//  Find the entry named "b.txt".
oEntry := CreateObject("Chilkat.ZipEntry")

nSuccess := oZip2:EntryOf("b.txt", oEntry)
IF (nSuccess == 0)
    ? oZip2:LastErrorText
    oZip:destroy()
    oZip2:destroy()
    oEntry:destroy()
    RETURN
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.
nSuccess := oZip2:DeleteEntry(oEntry)
IF (nSuccess == 0)
    ? oZip2:LastErrorText
    oZip:destroy()
    oZip2:destroy()
    oEntry:destroy()
    RETURN
ENDIF

//  Write the modified ZIP archive to a new file.
oZip2:FileName := "modified.zip"

nSuccess := oZip2:WriteZipAndClose()
IF (nSuccess == 0)
    ? oZip2:LastErrorText
    oZip:destroy()
    oZip2:destroy()
    oEntry:destroy()
    RETURN
ENDIF

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

? "ZIP archive updated successfully."

oZip:destroy()
oZip2:destroy()
oEntry:destroy()