code: public static void encrypt(Path path, InputStream publicKeyStream, OutputStream targetStream) throws Exception {
          PGPPublicKey publicKey = getPublicKey(publicKeyStream);
  
          try(ByteArrayOutputStream compressed = new ByteArrayOutputStream();
              ArmoredOutputStream armOut = new ArmoredOutputStream(targetStream)) {
              PGPCompressedDataGenerator compressedGenerator = new PGPCompressedDataGenerator(PGPCompressedData.ZIP);
              PGPUtil.writeFileToLiteralData(compressedGenerator.open(compressed), PGPLiteralData.BINARY, path.toFile());
              compressedGenerator.close();
  
              JcePGPDataEncryptorBuilder encryptorBuilder = new JcePGPDataEncryptorBuilder(PGPEncryptedData.CAST5)
                  .setWithIntegrityPacket(true).setSecureRandom(new SecureRandom()).setProvider(PROVIDER);
              PGPEncryptedDataGenerator dataGenerator = new PGPEncryptedDataGenerator(encryptorBuilder);
              JcePublicKeyKeyEncryptionMethodGenerator methodGenerator = new JcePublicKeyKeyEncryptionMethodGenerator
                  (publicKey).setProvider(PROVIDER).setSecureRandom(new SecureRandom());
              dataGenerator.addMethod(methodGenerator);
  
              byte[] compressedData = compressed.toByteArray();
              OutputStream encryptedOut = dataGenerator.open(armOut, compressedData.length);
              encryptedOut.write(compressedData);
              encryptedOut.close();
          }
      }
docs: Performs encryption on a single file using a PGP public key.
  @param path file to be encrypted
  @param publicKeyStream stream providing the encoded public key
  @param targetStream stream to receive the encrypted data
  @throws IOException if there is an error reading or writing from the streams
  @throws PGPException if the encryption process fails