I just solved exactly the same problem (I need to unpack zip file stored on JimFS filesystem)
There is a problem with Robert's response. ZipFile
and ZipInputStream
similar, but not equal. One of big disadvantage of ZipInputStream - it's doesn't fail in case of wrong or damaged zip file. It just return null on first nextEntry
call. For example following test will pass without errors
@Test
fun testZip() {
val randomBytes = Random.nextBytes(1000)
ZipInputStream(ByteArrayInputStream(randomBytes)).use { zis ->
var zipEntry = zis.nextEntry
while (zipEntry != null) {
println(zipEntry.name)
zipEntry = zis.nextEntry
}
}
}
So, if in case of zip files obtained from user input or by unstable network, there isn't any chance to check zip using ZipInputStream
. The only option here - to use Java's FileSystems
. The following test will fail with error zip END header not found
. With correct zip file newFileSystem works fine, of course.
@Test
fun testZipFS() {
val randomBytes = Random.nextBytes(1000)
val tmpFile = Files.createTempFile(Path.of("/tmp"), "tmp", ".zip")
Files.write(tmpFile, randomBytes)
val zipFs = FileSystems.newFileSystem(srcZip, null)
val zipRoot = zipFs.rootDirectories.single()
Files.walk(zipRoot).forEach { p ->
println(p.absolutePathString())
}
}