There are multiple files in a .zip file, which I'm trying to get. Trying to unzip the files provides a java.lang.IllegalStateException: zis.nextEntry must not be null. How to do it the right way?
@Throws(IOException::class)
fun unzip(zipFile: File, targetDirectory: File) {
val zis = ZipInputStream(
BufferedInputStream(FileInputStream(zipFile)))
try {
var ze: ZipEntry
var count: Int
val buffer = ByteArray(8192)
ze = zis.nextEntry
while (ze != null) {
val file = File(targetDirectory, ze.name)
val dir = if (ze.isDirectory) file else file.parentFile
if (!dir.isDirectory && !dir.mkdirs())
throw FileNotFoundException("Failed to ensure directory: " + dir.absolutePath)
if (ze.isDirectory)
continue
val fout = FileOutputStream(file)
try {
count = zis.read(buffer)
while (count != -1) {
fout.write(buffer, 0, count)
count = zis.read(buffer)
}
} finally {
fout.close()
zis.closeEntry()
ze = zis.nextEntry
}
}
} finally {
zis.closeEntry()
zis.close()
}
}