sje
sje

Reputation: 315

WCF returning "dynamic" gzipstream

I need to create a service that returns a GZipStream consisting of one or more files. The number of files could be hundreds and each file could potentially take up more than 500MB.

Is it somehow possible to add the files dynamically to the gzipstream as the stream is being transfered? (to avoid running into an out-of-memory exception when the files needs to be copied into the stream)

Etc:

  1. Copy fileA to the stream being returned.
  2. The client starts reading the stream.
  3. When fileA has been read (client side), copy fileB to the stream (server side).
  4. The client continue to read the stream.

... and so on until there's no more files.

Btw. it's not important that the files are compressed, just that they are combined into a zip file so that the client only has to download one single file.

So my goal is: Stream multiple files back to the client as one single file without processing all the files at once on the server (to avoid loading all files into memory and therefore raise an out-of-memory exception).

Could this be done by creating a custom stream somehow or is there an easier way to go?

Thanks.

Upvotes: 4

Views: 353

Answers (1)

Shiraz Bhaiji
Shiraz Bhaiji

Reputation: 65411

You could combine the files to a single zip file on disk and then stream that file back.

For how to combined the files to a zip file see: c# sharpziplib adding file to existing archive

This solves the out of memory problem, but it does mean that you need a lot of disk space.

Upvotes: 1

Related Questions