Android Kotlin compresses multiple files into zip

If there is a directory, add / to the entry name and recursively analyze it

Now assume that there are url of a series of images in the imgUrls array, we download them and name them according to index and compress them
getHttpContent() function details, see Android Kotlin use GET method to download data to ByteArray

fun packImgs(f: File): Boolean{
    f.parentFile?.let { if(!it.exists()) it.mkdirs() }
    if(!f.exists()) f.createNewFile()
    val zip = ZipOutputStream(CheckedOutputStream(f. outputStream(), CRC32()))
    zip. setLevel(9)
    var succeed = true
    for(i in imgUrls){
        zip.putNextEntry(ZipEntry("${imgUrls.indexOf(i)}.jpg"))val s = getHttpContent(i) ?.let { zip.write(it); true }?:false
        if(!s) succeed = s
        zip. flush()
    zip. close()
    return succeed

Leave a Reply

Your email address will not be published. Required fields are marked *