All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.eidu.content.learningpackages.LearningPackageWriter.kt Maven / Gradle / Ivy

The newest version!
package com.eidu.content.learningpackages

import com.eidu.content.learningpackages.domain.LearningUnitList
import com.eidu.content.learningpackages.util.json
import java.io.File
import java.io.FileOutputStream
import java.io.InputStream
import java.util.zip.ZipEntry
import java.util.zip.ZipOutputStream

class LearningPackageWriter {
    fun write(
        out: File,
        apk: File,
        units: LearningUnitList,
        icons: Iterable,
        assetsDirectory: File? = null
    ) {
        try {
            ZipOutputStream(FileOutputStream(out)).use { zip ->
                zip.putEntry("app.apk", apk.inputStream())

                zip.putEntry(
                    "units.json",
                    json.encodeToString(LearningUnitList.serializer(), units).toByteArray().inputStream()
                )

                icons.forEach { zip.putEntry("icons/${it.name}", it.inputStream()) }

                assetsDirectory?.apply {
                    walk().filter { it.isFile }
                        .forEach { zip.putEntry("assets/${it.toRelativeString(assetsDirectory)}", it.inputStream()) }
                }
            }
        } catch (e: Exception) {
            out.delete()
            throw e
        }
    }

    private fun ZipOutputStream.putEntry(path: String, contents: InputStream) {
        putNextEntry(ZipEntry(path))
        contents.use { it.copyTo(this) }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy