When the DeflateStream decompresses the entire compressed data is its position property is equal the size of compressed data?

Mar 30, 2011 at 12:53 PM

I’m going to use the DeflateStream class in the following way:

1)    Compress something to an underlying stream via DeflateStream.

2)    Add some another data to the underlying stream

3)    Set position of the underlying stream to 0.

4)    Decompress initial data.

And now the question – is the position of DeflateStream always equal to the size of the compressed data created on the step1. So, the data added on the step 2 can be safely read if I set the position of the underlying stream to the same value.

Here’s the example of the code.

               //1 var buf1 = new byte[100];
               using (var ds = new Ionic.Zlib.DeflateStream(underlyingStream, CompressionMode.Compress, true))
                   ds.Write(buf1,0, buf1.Length);
               var savedPosition = underlyingStream.Position;
               var buf2 = new byte[50];
               underlyingStream.Write(buf2, 0, buf2.Length);
               underlyingStream.Position = 0;
               var buf3 = new byte[buf1.Length];
               using (var ds = new Ionic.Zlib.DeflateStream(underlyingStream, CompressionMode.Decompress, true))
                   ds.Read(buf3, 0, buf3.Length);
                   if (savedPosition != ds.Position) throw new Exception("The initaial size of the compreassed data is not equal to the readed size.");