Sample code for 30+ languages & platforms
PowerBuilder 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 PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Zip
string ls_Charset
oleobject loo_Zip2
oleobject loo_Entry

li_Success = 0

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

loo_Zip = create oleobject
li_rc = loo_Zip.ConnectToNewObject("Chilkat.Zip")
if li_rc < 0 then
    destroy loo_Zip
    MessageBox("Error","Connecting to COM object failed")
    return
end if

li_Success = loo_Zip.NewZip("original.zip")
if li_Success = 0 then
    Write-Debug loo_Zip.LastErrorText
    destroy loo_Zip
    return
end if

ls_Charset = "utf-8"

li_Success = loo_Zip.AddString("a.txt","Contents of file A",ls_Charset)
if li_Success = 0 then
    Write-Debug loo_Zip.LastErrorText
    destroy loo_Zip
    return
end if

li_Success = loo_Zip.AddString("b.txt","Contents of file B",ls_Charset)
if li_Success = 0 then
    Write-Debug loo_Zip.LastErrorText
    destroy loo_Zip
    return
end if

li_Success = loo_Zip.AddString("c.txt","Contents of file C",ls_Charset)
if li_Success = 0 then
    Write-Debug loo_Zip.LastErrorText
    destroy loo_Zip
    return
end if

//  Write the ZIP archive to disk.
//  
//  The ZIP now contains:
//  
//      a.txt
//      b.txt
//      c.txt
//  
li_Success = loo_Zip.WriteZipAndClose()
if li_Success = 0 then
    Write-Debug loo_Zip.LastErrorText
    destroy loo_Zip
    return
end if

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

loo_Zip2 = create oleobject
li_rc = loo_Zip2.ConnectToNewObject("Chilkat.Zip")

li_Success = loo_Zip2.OpenZip("original.zip")
if li_Success = 0 then
    Write-Debug loo_Zip2.LastErrorText
    destroy loo_Zip
    destroy loo_Zip2
    return
end if

//  Find the entry named "b.txt".
loo_Entry = create oleobject
li_rc = loo_Entry.ConnectToNewObject("Chilkat.ZipEntry")

li_Success = loo_Zip2.EntryOf("b.txt",loo_Entry)
if li_Success = 0 then
    Write-Debug loo_Zip2.LastErrorText
    destroy loo_Zip
    destroy loo_Zip2
    destroy loo_Entry
    return
end if

//  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.
li_Success = loo_Zip2.DeleteEntry(loo_Entry)
if li_Success = 0 then
    Write-Debug loo_Zip2.LastErrorText
    destroy loo_Zip
    destroy loo_Zip2
    destroy loo_Entry
    return
end if

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

li_Success = loo_Zip2.WriteZipAndClose()
if li_Success = 0 then
    Write-Debug loo_Zip2.LastErrorText
    destroy loo_Zip
    destroy loo_Zip2
    destroy loo_Entry
    return
end if

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

Write-Debug "ZIP archive updated successfully."


destroy loo_Zip
destroy loo_Zip2
destroy loo_Entry