How to compress files in ZIP format in java?

Question:
How to compress files in ZIP format in java?

Code:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
 
public class CompressFile {
 
   public static void main(String[] args) {
 
      byte[] buffer = new byte[1024];
 
      try {
 
         FileOutputStream fos = new FileOutputStream("/tmp/myfile.zip");
         ZipOutputStream zos = new ZipOutputStream(fos);
         ZipEntry ze = new ZipEntry("myfile.txt");
         zos.putNextEntry(ze);
         FileInputStream in = new FileInputStream("/tmp/myfile.txt");
 
         int len;
         while ((len = in.read(buffer)) > 0) {
            zos.write(buffer, 0, len);
         }
 
         in.close();
         zos.closeEntry();
 
         zos.close();
 
         System.out.println("Finished");
 
      } catch (IOException ex) {
         System.out.println(ex);
      }
   }
}

Output:
$ java CompressFile
Finished

$ ls -l /tmp/myfile.*
-rw-rw-r-- 1 dennis dennis 6880 Feb 27 06:37 /tmp/myfile.txt
-rw-rw-r-- 1 dennis dennis  178 Feb 27 06:39 /tmp/myfile.zip