Skip to content

Instantly share code, notes, and snippets.

@PrashamTrivedi

PrashamTrivedi/ZipDownloader.kt Secret

Created Dec 30, 2017
Embed
What would you like to do?
Extract Zip files in kotlin... Works as Extract here function
suspend fun extractZipHere(zipFile: File) {
async(CommonPool) {
val zip = ZipFile(zipFile)
val extractionPath = zipFile.name.substring(0, zipFile.name.lastIndexOf("."))
for (zipEntry in zip.entries()) {
val parentFile = zipFile.parentFile
zipEntry?.let {
if (it.isDirectory) {
checkDir(parentFile, extractionPath)
} else {
val individualFile = File(parentFile, it.name)
val outputStream = FileOutputStream(individualFile)
val inputStream = zip.getInputStream(it)
try {
val readBytes = inputStream.readBytes()
outputStream.write(readBytes)
} catch (e: Exception) {
e.printStackTrace()
} finally {
inputStream.close()
outputStream.close()
}
}
}
}
}
}
private fun checkDir(parentFile: File, extractionPath: String) {
val file = File(parentFile, extractionPath)
if (!file.exists() || !file.isDirectory) {
file.mkdirs()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.