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

Lua
local chilkat = require("chilkat")

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

local zip = chilkat.Zip.new()

local success = zip:new_zip("original.zip")
if not success then
    print(zip.last_error_text)
    return
end

local charset = "utf-8"

success = zip:add_string("a.txt", "Contents of file A", charset)
if not success then
    print(zip.last_error_text)
    return
end

success = zip:add_string("b.txt", "Contents of file B", charset)
if not success then
    print(zip.last_error_text)
    return
end

success = zip:add_string("c.txt", "Contents of file C", charset)
if not success then
    print(zip.last_error_text)
    return
end

-- Write the ZIP archive to disk.
--
-- The ZIP now contains:
--
--     a.txt
--     b.txt
--     c.txt
--
success = zip:write_zip_and_close()
if not success then
    print(zip.last_error_text)
    return
end

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

local zip2 = chilkat.Zip.new()

success = zip2:open_zip("original.zip")
if not success then
    print(zip2.last_error_text)
    return
end

-- Find the entry named "b.txt".
local entry = chilkat.ZipEntry.new()

success = zip2:entry_of("b.txt", entry)
if not success then
    print(zip2.last_error_text)
    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.
success = zip2:delete_entry(entry)
if not success then
    print(zip2.last_error_text)
    return
end

-- Write the modified ZIP archive to a new file.
zip2.file_name = "modified.zip"

success = zip2:write_zip_and_close()
if not success then
    print(zip2.last_error_text)
    return
end

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

print("ZIP archive updated successfully.")