From 4967c0ef7092361698211c01613e985302deccac Mon Sep 17 00:00:00 2001 From: Gwendal Date: Fri, 4 Jan 2019 13:29:12 +0100 Subject: [PATCH] Add percentage progress during downloads --- src/main/kotlin/bandcampcollectiondownloader/Util.kt | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/bandcampcollectiondownloader/Util.kt b/src/main/kotlin/bandcampcollectiondownloader/Util.kt index adac02b..f29a236 100644 --- a/src/main/kotlin/bandcampcollectiondownloader/Util.kt +++ b/src/main/kotlin/bandcampcollectiondownloader/Util.kt @@ -5,8 +5,10 @@ import java.net.HttpURLConnection import java.net.URL import java.nio.file.Path import java.nio.file.Paths +import java.text.DecimalFormat import javax.mail.internet.ContentDisposition + const val BUFFER_SIZE = 4096 /** @@ -31,6 +33,7 @@ fun downloadFile(fileURL: String, saveDir: Path, optionalFileName: String = ""): } else -> Paths.get(url.file).fileName.toString() } + val fileSize: Int = httpConn.getHeaderField("Content-Length").toInt() // opens input stream from the HTTP connection val inputStream = httpConn.inputStream @@ -42,11 +45,17 @@ fun downloadFile(fileURL: String, saveDir: Path, optionalFileName: String = ""): val buffer = ByteArray(BUFFER_SIZE) var bytesRead = inputStream.read(buffer) + var total = 0 while (bytesRead != -1) { + val percent = total.toDouble() / fileSize.toDouble() * 100 + val formatter = DecimalFormat("#0.00") + val percentString = formatter.format(percent) + System.out.print("Progress: $percentString % ($total / $fileSize) \r") outputStream.write(buffer, 0, bytesRead) bytesRead = inputStream.read(buffer) + total += bytesRead } - + System.out.print(" ".repeat(120) + "\r") outputStream.close() inputStream.close() httpConn.disconnect()