Skip to content

Instantly share code, notes, and snippets.

@PrashamTrivedi
Created December 30, 2017 08:15
Show Gist options
  • Save PrashamTrivedi/01de7db088ead1a37022ee68bf736df4 to your computer and use it in GitHub Desktop.
Save PrashamTrivedi/01de7db088ead1a37022ee68bf736df4 to your computer and use it in GitHub Desktop.
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