-
-
Save PrashamTrivedi/01de7db088ead1a37022ee68bf736df4 to your computer and use it in GitHub Desktop.
Extract Zip files in kotlin... Works as Extract here function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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