You can wrap the DeflateStream in a stream of your own. Everytime you want to read from the compressing stream, you have to feed bytes into the deflatestream, until it writes to a buffer. You can then return bytes from that buffer.
public class CompressingStream : Stream
{
private readonly DeflateStream _deflateStream;
private readonly MemoryStream _buffer;
private Stream _inputStream;
private readonly byte[] _fileBuffer = new byte[64 * 1024];
public CompressingStream(Stream inputStream)
{
_inputStream = inputStream;
_buffer = new MemoryStream();
_deflateStream = new DeflateStream(_buffer, CompressionMode.Compress, true);
}
public override int Read(byte[] buffer, int offset, int count)
{
while (true)
{
var read = _buffer.Read(buffer, offset, count);
if (read > 0) return read;
if (_inputStream == null) return 0;
_buffer.Position = 0;
read = _inputStream.Read(_fileBuffer, 0, _fileBuffer.Length);
if (read == 0)
{
_inputStream.Close();
_inputStream = null;
_deflateStream.Close();
}
else
{
_deflateStream.Write(_fileBuffer, 0, read);
}
_buffer.SetLength(_buffer.Position);
_buffer.Position = 0;
}
}
public override bool CanRead
{
get { return true; }
}
#region Remaining overrides...
}
Whenever wcf reads from the stream, the compressing stream will write to compressing DeflateStream, until it kan read from the output buffer (_buffer).
It's ugly, but it works.
new DeflateStream(fileStream, CompressionMode.Compress)
does not work? – dtb