2
votes

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.

1
Why do you want to compress stream and then decompress it?Ahmed KRAIEM
Essentially I need to compress the file and store it as .gz in my file system...Rob Schneider
Then why are you using gzs with CompressionMode.Decompress?Ahmed KRAIEM
My bad, that was the wrong chunk of code...Rob Schneider
I was about to post an answer about how to write to a compressed file but after you have changed you question you are doing it correctly and as far as I can see you should no longer encounter the error you describe.Martin Liversage

1 Answers

1
votes

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);
}