Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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

66%
+2 −0
Q&A Pipeline for zipping multiple file chanel into a single output channel

I am in need of downloading some files (from dynamic list of URLs) and archive them into a single zip file. Since these files can be arbitrarily sized (totaling hundreds of MB), I want to avoid dow...

1 answer  ·  posted 8mo ago by tmpod‭  ·  last activity 8mo ago by tmpod‭

#2: Post edited by user avatar tmpod‭ · 2023-09-12T00:24:26Z (8 months ago)
Fix used code, was out of date and even more wrong
  • I am in need of downloading some files (from dynamic list of URLs) and archive them into a single zip file. Since these files can be arbitrarily sized (totaling hundreds of MB), I want to avoid downloading each entry individually, creating the entire archive in-memory and then uploading it, but rather make a sort of streamed pipeline for it.
  • In short, I want to have something going through the list of URLs, streaming each file into a `ZipOutputStream` (adding a new entry per file), and another thing consuming that stream (ktor multipart request, for example).
  • I have tried creating a [`ByteChannel`](https://api.ktor.io/ktor-io/io.ktor.utils.io/-byte-channel.html) and passing [`.toOutputStream()`](https://api.ktor.io/ktor-io/io.ktor.utils.io.jvm.javaio/to-output-stream.html) to a [`ZipOutputStream`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/zip/ZipOutputStream.html). Then, spawn a background job to grab each URL, create a zip entry and write packets into the stream, and finally return the channel wrapper in a [`ChannelProvider`](https://api.ktor.io/ktor-client/ktor-client-core/io.ktor.client.request.forms/-channel-provider/index.html) to pass into [`FormBuilder#append`](https://api.ktor.io/ktor-client/ktor-client-core/io.ktor.client.request.forms/-form-builder/append.html).
  • However, sometimes ktor times out, sometimes it doesn't and I manage to upload the archive as a multipart request, but it is corrupt.
  • Has anyone tried anything similar? If so, how have you tackled it?
  • Here's the code I have:
  • ```kotlin
  • ChannelProvider {
  • val fileChannel = ByteChannel()
  • val zos = ZipOutputStream(fileChannel.toOutputStream())
  • coroScope.launch {
  • files.forEach { f ->
  • httpClient.get(f.url).reader {
  • while (!channel.isClosedForRead) {
  • channel.readRemaining(DEFAULT_BUFFER_SIZE.toLong()).let {
  • zos.writePacket(it)
  • }
  • }
  • }
  • }
  • }
  • fileChannel
  • }
  • ```
  • I am in need of downloading some files (from dynamic list of URLs) and archive them into a single zip file. Since these files can be arbitrarily sized (totaling hundreds of MB), I want to avoid downloading each entry individually, creating the entire archive in-memory and then uploading it, but rather make a sort of streamed pipeline for it.
  • In short, I want to have something going through the list of URLs, streaming each file into a `ZipOutputStream` (adding a new entry per file), and another thing consuming that stream (ktor multipart request, for example).
  • I have tried creating a [`ByteChannel`](https://api.ktor.io/ktor-io/io.ktor.utils.io/-byte-channel.html) and passing [`.toOutputStream()`](https://api.ktor.io/ktor-io/io.ktor.utils.io.jvm.javaio/to-output-stream.html) to a [`ZipOutputStream`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/zip/ZipOutputStream.html). Then, spawn a background job to grab each URL, create a zip entry and write packets into the stream, and finally return the channel wrapper in a [`ChannelProvider`](https://api.ktor.io/ktor-client/ktor-client-core/io.ktor.client.request.forms/-channel-provider/index.html) to pass into [`FormBuilder#append`](https://api.ktor.io/ktor-client/ktor-client-core/io.ktor.client.request.forms/-form-builder/append.html).
  • However, sometimes ktor times out, sometimes it doesn't and I manage to upload the archive as a multipart request, but it is corrupt.
  • Has anyone tried anything similar? If so, how have you tackled it?
  • Here's the code I have:
  • ```kotlin
  • ChannelProvider {
  • val fileChannel = ByteChannel()
  • val zos = ZipOutputStream(fileChannel.toOutputStream())
  • coroScope.launch {
  • files.forEach { f ->
  • httpClient.get(f.url).reader {
  • zos.putNextEntry(ZipEntry(f.filename))
  • while (!channel.isClosedForRead) {
  • channel.readRemaining(DEFAULT_BUFFER_SIZE.toLong()).let {
  • zos.writePacket(it)
  • }
  • }
  • zos.closeEntry()
  • }
  • }
  • zos.close()
  • fileChannel.close()
  • }
  • fileChannel
  • }
  • ```
#1: Initial revision by user avatar tmpod‭ · 2023-09-11T23:47:23Z (8 months ago)
Pipeline for zipping multiple file chanel into a single output channel
I am in need of downloading some files (from dynamic list of URLs) and archive them into a single zip file. Since these files can be arbitrarily sized (totaling hundreds of MB), I want to avoid downloading each entry individually, creating the entire archive in-memory and then uploading it, but rather make a sort of streamed pipeline for it.  
In short, I want to have something going through the list of URLs, streaming each file into a `ZipOutputStream` (adding a new entry per file), and another thing consuming that stream (ktor multipart request, for example).

I have tried creating a [`ByteChannel`](https://api.ktor.io/ktor-io/io.ktor.utils.io/-byte-channel.html) and passing [`.toOutputStream()`](https://api.ktor.io/ktor-io/io.ktor.utils.io.jvm.javaio/to-output-stream.html) to a [`ZipOutputStream`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/zip/ZipOutputStream.html). Then, spawn a background job to grab each URL, create a zip entry and write packets into the stream, and finally return the channel wrapper in a [`ChannelProvider`](https://api.ktor.io/ktor-client/ktor-client-core/io.ktor.client.request.forms/-channel-provider/index.html) to pass into [`FormBuilder#append`](https://api.ktor.io/ktor-client/ktor-client-core/io.ktor.client.request.forms/-form-builder/append.html).  
However, sometimes ktor times out, sometimes it doesn't and I manage to upload the archive as a multipart request, but it is corrupt.

Has anyone tried anything similar? If so, how have you tackled it?

Here's the code I have:
```kotlin
ChannelProvider {
    val fileChannel = ByteChannel()
    val zos = ZipOutputStream(fileChannel.toOutputStream())
    coroScope.launch {
        files.forEach { f ->
            httpClient.get(f.url).reader {
                while (!channel.isClosedForRead) {
                    channel.readRemaining(DEFAULT_BUFFER_SIZE.toLong()).let {
                        zos.writePacket(it)
                    }
                }
            }
        }
    }
    fileChannel
}
```