2

I have a gzipstream that is compressed and I want to write it to a file. Now the problem is that the Read is not supported on the gzipstream that is compressed. Below is my code where the gzipstream reads the stream from a memorystream and then I want to write it to a filestream.

using (var stream = new MemoryStream())
{
    using (FileStream file = new FileStream(@"c:\newest.xml.gz", 
        FileMode.Create, FileAccess.Write))
    {
        using (GZipStream gzs = new GZipStream(file, CompressionLevel.Fastest))
        {
            stream.CopyTo(gzs);
        }
    }  
} 

Any idea how I can create a filestream from a compressed gzipstream?

EDIT:

That was my bad, sorry to have waste your time. For future reference the code above should work, the problem was somewhere else.

gunr2171
  • 16,104
  • 25
  • 61
  • 88
Rob Schneider
  • 679
  • 4
  • 13
  • 27

1 Answers1

1

You seem to be reading and writing the same memory stream. I don't think that's possible; you should use two different streams: one from which you read, and another into which you write:

using (gzipStream = new GZipStream(writeStream,CompressionLevel.Fastest))
{
    readStream.CopyTo(gzipStream);
}
Joni
  • 108,737
  • 14
  • 143
  • 193