Sample code for 30+ languages & platforms
AutoIt

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

AutoIt
Local $bSuccess = False

$bSuccess = False

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

$oZip = ObjCreate("Chilkat.Zip")

$bSuccess = $oZip.NewZip("original.zip")
If ($bSuccess = False) Then
    ConsoleWrite($oZip.LastErrorText & @CRLF)
    Exit
EndIf

Local $sCharset = "utf-8"

$bSuccess = $oZip.AddString("a.txt","Contents of file A",$sCharset)
If ($bSuccess = False) Then
    ConsoleWrite($oZip.LastErrorText & @CRLF)
    Exit
EndIf

$bSuccess = $oZip.AddString("b.txt","Contents of file B",$sCharset)
If ($bSuccess = False) Then
    ConsoleWrite($oZip.LastErrorText & @CRLF)
    Exit
EndIf

$bSuccess = $oZip.AddString("c.txt","Contents of file C",$sCharset)
If ($bSuccess = False) Then
    ConsoleWrite($oZip.LastErrorText & @CRLF)
    Exit
EndIf

; Write the ZIP archive to disk.
; 
; The ZIP now contains:
; 
;     a.txt
;     b.txt
;     c.txt
; 
$bSuccess = $oZip.WriteZipAndClose()
If ($bSuccess = False) Then
    ConsoleWrite($oZip.LastErrorText & @CRLF)
    Exit
EndIf

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

$oZip2 = ObjCreate("Chilkat.Zip")

$bSuccess = $oZip2.OpenZip("original.zip")
If ($bSuccess = False) Then
    ConsoleWrite($oZip2.LastErrorText & @CRLF)
    Exit
EndIf

; Find the entry named "b.txt".
$oEntry = ObjCreate("Chilkat.ZipEntry")

$bSuccess = $oZip2.EntryOf("b.txt",$oEntry)
If ($bSuccess = False) Then
    ConsoleWrite($oZip2.LastErrorText & @CRLF)
    Exit
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.
$bSuccess = $oZip2.DeleteEntry($oEntry)
If ($bSuccess = False) Then
    ConsoleWrite($oZip2.LastErrorText & @CRLF)
    Exit
EndIf

; Write the modified ZIP archive to a new file.
$oZip2.FileName = "modified.zip"

$bSuccess = $oZip2.WriteZipAndClose()
If ($bSuccess = False) Then
    ConsoleWrite($oZip2.LastErrorText & @CRLF)
    Exit
EndIf

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

ConsoleWrite("ZIP archive updated successfully." & @CRLF)