Search code examples
c#wcfstreamcompressiondeflate

DeflateStream: compress files while reading


I'm trying to send a big chunk of data through WCF (some GBs). I would like to compress the file while reading it using Streams, but looks like DeflateStream has two modes:

  • Compress (writes on the stream)
  • Decompress (reads the stream)

None of these modes works in my case. I would like to read an un-compressed file from disk and return a compressed stream through WCF.

Is there a way to do so, or I have to use a temporal file (or MemoryStream)?

Is a missing feature or is just not possible for some reason?


Solution

  • It looks like you are trying to compress while reading the file. The way deflatestream is written, compression has to happen as part of a write. Try wrapping the stream that you are sending over the wire, not the stream that you are reading off disk. If they are the same, you need an intermediate stream.