Sample code for 30+ languages & platforms
Java

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

Java
import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

  public static void main(String argv[])
  {
    boolean success = false;

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

    CkZip zip = new CkZip();

    success = zip.NewZip("original.zip");
    if (success == false) {
        System.out.println(zip.lastErrorText());
        return;
        }

    String charset = "utf-8";

    success = zip.AddString("a.txt","Contents of file A",charset);
    if (success == false) {
        System.out.println(zip.lastErrorText());
        return;
        }

    success = zip.AddString("b.txt","Contents of file B",charset);
    if (success == false) {
        System.out.println(zip.lastErrorText());
        return;
        }

    success = zip.AddString("c.txt","Contents of file C",charset);
    if (success == false) {
        System.out.println(zip.lastErrorText());
        return;
        }

    //  Write the ZIP archive to disk.
    //  
    //  The ZIP now contains:
    //  
    //      a.txt
    //      b.txt
    //      c.txt
    //  
    success = zip.WriteZipAndClose();
    if (success == false) {
        System.out.println(zip.lastErrorText());
        return;
        }

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

    CkZip zip2 = new CkZip();

    success = zip2.OpenZip("original.zip");
    if (success == false) {
        System.out.println(zip2.lastErrorText());
        return;
        }

    //  Find the entry named "b.txt".
    CkZipEntry entry = new CkZipEntry();

    success = zip2.EntryOf("b.txt",entry);
    if (success == false) {
        System.out.println(zip2.lastErrorText());
        return;
        }

    //  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.DeleteEntry(entry);
    if (success == false) {
        System.out.println(zip2.lastErrorText());
        return;
        }

    //  Write the modified ZIP archive to a new file.
    zip2.put_FileName("modified.zip");

    success = zip2.WriteZipAndClose();
    if (success == false) {
        System.out.println(zip2.lastErrorText());
        return;
        }

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

    System.out.println("ZIP archive updated successfully.");
  }
}