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

Create a Zip Entirely in Memory

See more Zip Examples

Demonstrates how to create a .zip from in-memory byte data and strings, and to write the .zip to an in-memory image.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.BinData;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Crypt2;
import com.chilkatsoft.chilkat2.FileAccess;
import com.chilkatsoft.chilkat2.Zip;

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.

        Crypt2 crypt = new Crypt2();
        Zip zip = new Zip();

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

        // Add the bytes 0x00 0x01 0x02 0x03 ... 0x0F as a file in the .zip
        BinData bd = new BinData();
        bd.appendEncoded("000102030405060708090A0B0C0D0E0F", "hex");
        zip.addBd("binaryData.dat", bd);

        // Add the string "Hello World!" to the .zip
        zip.addString("helloWorld.txt", "Hello World!", "utf-8");

ERROR: Invalid type for Chilkat2 Java: pbytes
ERROR: Method not in Chilkat2 Java: Zip.WriteToMemory
        zipFileInMemory = zip.ERROR();

        // We could save these files to a file, and it is a valid .zip
        FileAccess fac = new FileAccess();
ERROR: Method not in Chilkat2 Java: FileAccess.WriteEntireFile
        success = fac.ERROR();
        if (!success) {
            System.out.println(fac.getLastErrorText());
            return;
        }

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