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

Change a Filename before Unzipping

See more Zip Examples

How to open a zip and modify the filename of one or more files within the zip before unzipping.

Chilkat Java Downloads

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

public class ChilkatExample {

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

        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Zip zip = new Zip();

        try {
            zip.openZip("test.zip");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        ZipEntry entry = new ZipEntry();

        success = zip.entryOf("hamlet.xml", entry);
        if (!success) {
            System.out.println(zip.getLastErrorText());
            return;
        }

        entry.setFileName("hamlet2.xml");

        success = zip.entryOf("helloWorld.pl", entry);
        if (!success) {
            System.out.println(zip.getLastErrorText());
            return;
        }

        entry.setFileName("hw.pl");

        // Now unzip to the "test" subdirectory, under our current
        // working directory:
        int numFilesUnzipped = zip.unzip("test");
        if (numFilesUnzipped < 0) {
            System.out.println(zip.getLastErrorText());
            return;
        }

        // The filenames within the .zip are unchanged, but it unzipped
        // test/hw.pl and test/hamlet2.xml
    }
}