Sample code for 30+ languages & platforms
Java

Zip a Directory Tree

See more Zip Examples

Demonstrates how to zip an entire directory tree into a .zip archive.

Chilkat Java Downloads

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

public class ChilkatExample {

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

        Zip zip = new Zip();

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

        // Append a directory tree.  The call to AppendFiles does
        // not read the file contents or append them to the zip
        // object in memory.  It simply appends references
        // to the files so that when WriteZip or WriteZipAndClose
        // is called, the referenced files are streamed and compressed
        // into the .zip output file.

        boolean recurse = true;
        try {
            zip.appendFiles("c:/temp/a/*", recurse);

            zip.writeZipAndClose();
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("Zip Created!");
    }
}