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

Replace/Update a FIle in a .zip

See more Zip Examples

Demonstrates how to replace/update a file from a .zip. Note: This requires the entire .zip to be rewritten.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.Zip;
import com.chilkatsoft.chilkat2.ZipEntry;

public class ChilkatExample {

    public static void main(String[] args) {
        // This requires the Chilkat Zip API to have been previously unlocked.
        // See Unlock Chilkat Zip for sample code.

        // First prepare a .zip and write it..
        Zip zip = new Zip();

        zip.newZip("qa_output/abc.zip");

        // Add some files..
        String charset = "utf-8";
        zip.addString("a.txt", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", charset);
        zip.addString("b.txt", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", charset);
        zip.addString("c.txt", "cccccccccccccccccccccccccccccccccccc", charset);

        // Write to qa_output/abc.zip
        // This .zip contains three files: a.txt, b.txt, and c.txt
        zip.writeZipAndClose();

        // -------------------------------------------------------------------
        // Open abc.zip, replace the content of the "b.txt" entry with something else, and re-write.
        Zip zip2 = new Zip();
        zip2.openZip("qa_output/abc.zip");

        ZipEntry entry = new ZipEntry();
        if (zip2.entryOf("b.txt", entry)) {
            entry.replaceString("This is the new content.  bbbbbbbbbbbbbbbbbbbbbb", "utf-8");
        }

        // Write the modified .zip back to "abc.zip"
        zip2.writeZipAndClose();

        System.out.println("success.");
    }
}