From 63057c0ac40e264f959017d9c91a6e2b3fccc3ac Mon Sep 17 00:00:00 2001 From: Timo Ley Date: Tue, 28 Jul 2020 16:42:39 +0200 Subject: [PATCH] Added Zip to Util.kt --- .../ley/anvil/modpacktools/util/Util.kt | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/ley/anvil/modpacktools/util/Util.kt b/src/main/kotlin/ley/anvil/modpacktools/util/Util.kt index 1de96dd..8b444ac 100644 --- a/src/main/kotlin/ley/anvil/modpacktools/util/Util.kt +++ b/src/main/kotlin/ley/anvil/modpacktools/util/Util.kt @@ -9,11 +9,12 @@ import okhttp3.MediaType import okhttp3.MediaType.Companion.toMediaType import okhttp3.Request import okhttp3.RequestBody.Companion.toRequestBody -import java.io.File -import java.io.FileReader -import java.io.IOException +import java.io.* import java.net.URI import java.net.URL +import java.util.zip.ZipEntry +import java.util.zip.ZipOutputStream + /** * Reads a Json File @@ -87,4 +88,24 @@ fun URL.sanitize(): URL? { } catch(e: Exception) { null } +} + +fun zipDir(dir: File, parent: String?, zip: ZipOutputStream) { + for (file in dir.listFiles()){ + println(file.name) + if (file.isDirectory) { + zipDir(file, parent + file.name + "/", zip) + continue + } + zip.putNextEntry(ZipEntry(parent + file.name)) + var inp = BufferedInputStream(FileInputStream(file)) + var bytesRead: Long = 0 + val bytesIn = ByteArray(4096) + var read = 0 + while (inp.read(bytesIn).also { read = it } != -1) { + zip.write(bytesIn, 0, read) + bytesRead += read.toLong() + } + zip.closeEntry() + } } \ No newline at end of file