package com.example.myapplication.utils

import java.io.*
import java.util.zip.ZipEntry
import java.util.zip.ZipInputStream

fun unzipToDir(zipInputStream: InputStream, targetDir: File) {
    ZipInputStream(BufferedInputStream(zipInputStream)).use { zis ->
        var entry: ZipEntry? = zis.nextEntry
        val buffer = ByteArray(4096)

        while (entry != null) {
            val file = File(targetDir, entry.name)

            if (entry.isDirectory) {
                file.mkdirs()
            } else {
                file.parentFile?.mkdirs()
                FileOutputStream(file).use { fos ->
                    var count: Int
                    while (zis.read(buffer).also { count = it } != -1) {
                        fos.write(buffer, 0, count)
                    }
                }
            }

            zis.closeEntry()
            entry = zis.nextEntry
        }
    }
}
