momrak
12/02/2019, 4:20 PM@GetMapping(value = ["download-all"], produces = ["application/zip"])
fun downloadAll(): ByteArray {
val outputStream = ByteArrayOutputStream(1024)
val bufferedOutputStream = BufferedOutputStream(outputStream)
val zipOutputStream = ZipOutputStream(bufferedOutputStream)
val docIds = docService.getDocIds()
runBlocking {
val docStream = docIds
.map { GlobalScope.async { docService.getDoc(it) } }
.map { ByteArrayInputStream(it.await()) }
.map { BufferedInputStream(it) }
docStream.forEachIndexed { index, bufferedInputStream ->
val zipEntry = ZipEntry("test${index}.pdf")
zipOutputStream.putNextEntry(zipEntry)
bufferedInputStream.copyTo(zipOutputStream, 1024)
bufferedInputStream.close()
zipOutputStream.closeEntry()
}
zipOutputStream.close()
}
return outputStream.toByteArray()
}
Dominaezzz
12/02/2019, 6:19 PM@GetMapping(value = ["download-all"], produces = ["application/zip"])
fun downloadAll(): ByteArray {
val outputStream = ByteArrayOutputStream(1024)
runBlocking {
val docBytes = docService.getDocIds()
.map { async { docService.getDoc(it) } }
.awaitAll()
ZipOutputStream(outputStream).use { output ->
docBytes.forEachIndexed { index, bytes ->
val zipEntry = ZipEntry("test${index}.pdf")
output.putNextEntry(zipEntry)
output.write(bytes)
output.closeEntry()
}
}
}
return outputStream.toByteArray()
}
momrak
12/03/2019, 8:25 AM