This Java tip illustrates a method of creating a ZIP file. ZIP is a universal format used by many applications to compress their files. By using this option developer may give their user an option of compressing their files in ZIP format.

  // These are the files to include in the ZIP file
    String[] source = new String[]{"source1", "source2"};
    // Create a buffer for reading the files
    byte[] buf = new byte[1024];
    try {
        // Create the ZIP file
        String target = "";
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(target));
        // Compress the files
        for (int i=0; i<source.length; i++) {
            FileInputStream in = new FileInputStream(source[i]);
            // Add ZIP entry to output stream.
            out.putNextEntry(new ZipEntry(source[i]));
            // Transfer bytes from the file to the ZIP file
            int len;
            while ((len = > 0) {
                out.write(buf, 0, len);
            // Complete the entry
        // Complete the ZIP file
    } catch (IOException e) {