GZipStream and DeflateStream does not decompress?

by Dominic Zukiewicz 31. October 2007 12:18
I spent hours looking over 4 or 5 lines of code concerning these compression/decompression streams to find out why it was compressing and not decompressing.

The problem resulted in the decompression stream returning a zero length byte array for the resulting data. The problem also replicated itself as being too long. What went wrong?

This works!

//Decompress
private static string Uncompress(byte[] compressedData)
{
     MemoryStream ms = new MemoryStream(compressedData);
     MemoryStream uncompressedStream = new MemoryStream();

     GZipStream gZip = new GZipStream(ms, CompressionMode.Uncompress);

     byte[] buffer = new byte[1024];
     int lengthOfBuffer = gZip.Read(0,buffer,buffer.Length);

     while( lengthOfBuffer != 0 )
     {
          uncompressedStream.Write(buffer,0,lengthOfBuffer);
          lengthOfBuffer = gZip.Read(0,buffer,buffer.Length);
     }

     return Encoding.Unicode.GetString( uncompressedStream.ToArray() );
}

Now for the subtle differences.
This returns back nothing, because you are not continuously reading from the stream. You need to pull more information out and write it to the MemoryStream.
private static string Uncompress(byte[] compressedData)
{
     MemoryStream ms = new MemoryStream();
     ms.Read(compressedData,0,compressedData.Length);
     //ms.Position = 0; <--- This was missing!! 

     MemoryStream uncompressedStream = new MemoryStream();

     GZipStream gZip = new GZipStream(ms, CompressionMode.Uncompress);

     byte[] buffer = new byte[1024];
     int lengthOfBuffer = gZip.Read(0,buffer,buffer.Length);

     while( lengthOfBuffer != 0 )
     {
          uncompressedStream.Write(buffer,0,lengthOfBuffer); 
                                                                //<-- Something is missing here! 
     }

     return Encoding.Unicode.GetString( uncompressedStream.ToArray() ); 
}

This returns a string that is not the same length as the original. The reason is that you always assume that the buffer is the same size. When it reaches the end of the stream, the buffer may only be half full, and therefore the following code will not work:

//Decompress
private static string Uncompress(byte[] compressedData)
{
     MemoryStream ms = new MemoryStream(compressedData);
     MemoryStream uncompressedStream = new MemoryStream();

     GZipStream gZip = new GZipStream(ms, CompressionMode.Uncompress);

     byte[] buffer = new byte[1024];
     int lengthOfBuffer = gZip.Read(0,buffer,buffer.Length);

     while( lengthOfBuffer != 0 )
     {
          uncompressedStream..Write(buffer,0,buffer.Length); //<-- Problem is here!
          lengthOfBuffer = gZip.Read(0,buffer,buffer.Length);
     }

     return Encoding.Unicode.GetString( uncompressedStream.ToArray() );
}

Check your code!!

Tags:

Framework

Comments

8/3/2009 3:45:59 PM #

This code makes absolutely no sense. The syntax incorrect and there are glaring errors throughout. This post is useless.

test |

8/3/2009 3:51:24 PM #

Hello "test",

Yes there are glaring errors, that is my intent. The blog entry is about common mistakes when using the GZipStream classes.

Dominic Zukiewicz |

Powered by BlogEngine.NET 1.5.0.7
Theme by Interakting

Interakting

A full service digital agency offering online strategy, design and usability, systems integration and online marketing services that deliver real business benefits and ensure your online objectives are met.

Calendar

<<  February 2012  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
2728291234
567891011

View posts in large calendar