kotlin download file from url

import java.io.FileOutputStream
import java.net.URL

fun main() {
    val url = "https://www.example.com/file.zip" // URL of the file to download
    val connection = URL(url).openConnection() // Open a connection to the URL
    val inputStream = connection.getInputStream() // Get input stream from the connection
    val fileOutputStream = FileOutputStream("downloaded_file.zip") // Create FileOutputStream to write the downloaded file
    val buffer = ByteArray(1024) // Create a buffer to read data in chunks

    var bytesRead: Int // Variable to store the number of bytes read

    // Read data from input stream into buffer and write to the file output stream
    while (inputStream.read(buffer).also { bytesRead = it } != -1) {
        fileOutputStream.write(buffer, 0, bytesRead)
    }

    // Close the streams
    inputStream.close()
    fileOutputStream.close()
}