Writing to a compression stream is not supported. Using System.IO.GZipStream

I am trying to unzip a file (.gz) using the GZipStream class which is included in the .NET framework. I use the MSDN documentation and I keep getting this exception: "Writing to the compression stream is not supported."

Here is the source of the application:

try { var infile = new FileStream(@"C:\TarDecomp\TarDecomp\TarDecomp\bin\Debug\nick_blah-2008.tar.gz", FileMode.Open, FileAccess.Read, FileShare.Read); byte[] buffer = new byte[infile.Length]; // Read the file to ensure it is readable. int count = infile.Read(buffer, 0, buffer.Length); if (count != buffer.Length) { infile.Close(); Console.WriteLine("Test Failed: Unable to read data from file"); return; } infile.Close(); MemoryStream ms = new MemoryStream(); // Use the newly created memory stream for the compressed data. GZipStream compressedzipStream = new GZipStream(ms, CompressionMode.Decompress, true); Console.WriteLine("Decompression"); compressedzipStream.Write(buffer, 0, buffer.Length); // Close the stream. compressedzipStream.Close(); Console.WriteLine("Original size: {0}, Compressed size: {1}", buffer.Length, ms.Length); 

An exception is thrown when ZipStream.write () is compressed.

Any ideas? What exception does this tell me? Thanks!

+6
c # compression gzipstream
source share
3 answers

It is reported that you should call Read instead of Write , as it is decompressed! Also, the memory stream must be constructed with data, or rather, you must pass the file stream directly to the GZipStream constructor.

An example of how this should have been done (did not try to compile it):

 Stream inFile = new FileStream(@"C:\TarDecomp\TarDecomp\TarDecomp\bin\Debug\nick_blah-2008.tar.gz", FileMode.Open, FileAccess.Read, FileShare.Read); Stream decodedStream = new MemoryStream(); byte[] buffer = new byte[4096]; using (Stream inGzipStream = new GZipStream(inFile, CompressionMode.Decompress)) { int bytesRead; while ((bytesRead = inGzipStream.Read(buffer, 0, buffer.Length)) > 0) decodedStream.Write(buffer, 0, bytesRead); } // Now decodedStream contains the decoded data 
+14
source share

The compression code does not work like encryption - you cannot decompress from one stream to another, writing compressed data. You must provide a stream that contains the compressed data, and let GZipStream read from it. Something like that:

 using (Stream file = File.OpenRead(filename)) using (Stream gzip = new GZipStream(file, CompressionMode.Decompress)) using (Stream memoryStream = new MemoryStream()) { CopyStream(gzip, memoryStream); return memoryStream.ToArray(); } 

CopyStream is a simple utility method for reading from one stream and copying all data to another. Something like that:

 static void CopyStream(Stream input, Stream output) { byte[] buffer = new byte[8192]; int bytesRead; while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0) { output.Write(buffer, 0, bytesRead); } } 
+9
source share

How compression streams work can be puzzling at first.

A read receives compressed data, and a write receives uncompressed data. In general, a stream ensures that you only โ€œseeโ€ uncompressed data at any time.

The right way to achieve what you are trying to do is read with GZipStream and then write with GZipStream as well.

+1
source share

All Articles