Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
Well, ain't this fun hehe Turns out I failed to see .reader launches its own job, which resulted in multiple coroutines contending for the zip stream, each trying to write an entry... 🤦 The solut...
Answer
#1: Initial revision
Well, ain't this fun hehe Turns out I failed to see [`.reader`](https://api.ktor.io/ktor-io/io.ktor.utils.io/reader.html) launches its own job, which resulted in multiple coroutines contending for the zip stream, each trying to write an entry... 🤦 The solution was just to get a channel and use that instead. Additionally, to mitigate the still blocking nature of Java's `ZipOutputStream`, I spawned the job in the appropriate [`Dispatchers.IO`](https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-dispatchers/-i-o.html). Here it is: ```kotlin ChannelProvider { val fileChannel = ByteChannel() val zos = ZipOutputStream(fileChannel.toOutputStream()) coroScope.launch(Dispatchers.IO) { files.forEach { f -> val body = httpClient.get(f.url).bodyAsChannel() zos.putNextEntry(ZipEntry(f.filename)) while (!body.isClosedForRead) { body.readRemaining(DEFAULT_BUFFER_SIZE.toLong()).let { zos.writePacket(it) } } zos.closeEntry() } zos.close() fileChannel.close() } fileChannel } ```