Why is my GZipStream not writeable?

Posted by Ozzah on Stack Overflow See other posts from Stack Overflow or by Ozzah
Published on 2011-11-17T01:44:19Z Indexed on 2011/11/17 1:51 UTC
Read the original article Hit count: 348

Filed under:
|
|
|

I have some GZ compressed resources in my program and I need to be able to write them out to temporary files for use. I wrote the following function to write the files out and return true on success or false on failure. In addition, I've put a try/catch in there which shows a MessageBox in the event of an error:

private static bool extractCompressedResource(byte[] resource, string path)
{
  try
  {
    using (MemoryStream ms = new MemoryStream(resource))
    {
      using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite))
      {
        using (GZipStream zs = new GZipStream(fs, CompressionMode.Decompress))
        {
          ms.CopyTo(zs); // Throws exception

          zs.Close();
          ms.Close();
        }
      }
    }
  }
  catch (Exception ex)
  {
    MessageBox.Show(ex.Message); // Stream is not writeable
    return false;
  }

  return true;
}

I've put a comment on the line which throws the exception. If I put a breakpoint on that line and take a look inside the GZipStream then I can see that it's not writeable (which is what's causing the problem).

Am I doing something wrong, or is this a limitation of the GZipStream class?

© Stack Overflow or respective owner

Related posts about c#

Related posts about compression