Android uses ZipFile to compress/decompress zip files

Original link: http://i.lckiss.com/?p=8242

Background & Requirements

A non-hierarchical minimalist compression, mainly used for application databases and SP import and export, and does not want to introduce three-party plug-in libraries.

accomplish

 object ZipManager {  
  
    fun zip(files: Array<File>, zipFile: File) {  
        ZipOutputStream(zipFile. outputStream ()). use { output ->  
            files.forEach { file ->  
                file.inputStream (). use { input ->  
                    val entry = ZipEntry( file.name )  
                    output. putNextEntry(entry)  
                    input. copyTo (output)  
                }  
}  
}  
    }  
  
    fun unzip(zipFile: File): String {  
        val parentFile = zipFile. parentFile ?: return ""  
        val outDir = "${parentFile. absolutePath }/${zipFile. nameWithoutExtension }/"  
        File(outDir). apply {  
            mkdirs()  
        }  
        ZipFile(zipFile). use { zip ->  
            zip.entries(). asSequence (). forEach { entry ->  
                zip.getInputStream(entry) .use { input ->  
                    val file = File(outDir + entry.name )  
                    file. outputStream (). use {  
                        input. copyTo ( it )  
                    }  
}  
}  
}  
        return outDir  
    }  
}

other

It should be easier to understand. Some Kotlin file stream syntax sugar is used, and the hierarchy is not supported because it is not supported. It is also possible to modify it yourself. It is a hierarchical nesting problem. The online library is a bit overkill. This is small and easy to use.

above!

This article is transferred from: http://i.lckiss.com/?p=8242
This site is only for collection, and the copyright belongs to the original author.