code: private void addDirectoryToZip(File topDirectory, File directory, ZipOutputStream out) throws IOException {
      Path top = Paths.get(topDirectory.getAbsolutePath());
  
      File[] files = directory.listFiles();
      byte[] buffer = new byte[BUFFER_SIZE];
  
      for (int i = 0; i < files.length; i++) {
        Path entry = Paths.get(files[i].getAbsolutePath());
        Path relative = top.relativize(entry);
        String entryName = relative.toString();
        if (files[i].isDirectory()) {
          entryName += FILE_SEPARATOR;
        }
  
        out.putNextEntry(new ZipEntry(entryName.replace("\\", "/")));
  
        if (!files[i].isDirectory()) {
          try (FileInputStream in = new FileInputStream(files[i])) {
            int bytesRead;
            while ((bytesRead = in.read(buffer)) != -1) {
              out.write(buffer, 0, bytesRead);
            }
          }
          out.closeEntry();
        }
  
        if (files[i].isDirectory()) {
          addDirectoryToZip(topDirectory, files[i], out);
        }
      }
    }
docs: Recursively adds directories to a zip file.