System.IO.Compression.Deflatestream allows one to read from a stream of compressed data, decompressing (inflating) it as it is read. However, it is not possible to read a stream of normal data, compressing it as it is read. (This throws an InvalidOperationException.)
Likewise, one can write normal data to a stream, compressing (deflating) the data as it is written. However, it is not possible to write compressed data to a stream, uncompressing it into normal data as it is written. (There is no exception documented for this, but presumably it's the same InvalidOperationException.)
In other words, data compression and decompression are supported in one direction only, writing to compress and reading to decompress. The reverse directions are not: writing to decompress and reading to compress.
Consider an application that reads plaintext data from a file, compresses it, then encrypts it, writing it to an output file (or network socket). The reverse operation reads the encrypted ciphertext data, decrypts it, then decompresses it, writing the recovered plaintext data to an output file (or socket).
The only work-arounds I can think of are:
1) Use an in-memory MemoryStream to hold the entire data stream. This is obviously impractical for large amounts of data or for streaming (network) data.
2) Write Stream classes that utilize a Deflatestream object, intercepting the Read and Write calls to handle the compression/decompression. The code operates in an inside-out manner.
3) Use temporary files to buffer large compressed/uncompressed data. Data to be read as compressed is first read and written compressed to a temp file, then the file is opened in read mode to read the compressed data, then the temp file is deleted. The opposite is done for data to be written as uncompressed.
None of these work-arounds is a good solution.