Reputation: 147
What could be wrong with those set of methods?
byte[] bytes;
using (var memory_stream = new MemoryStream())
using (var gzip_stream = new GZipStream(memory_stream, CompressionMode.Compress))
{
var buffer = Encoding.Default.GetBytes("Hello nurse!");
gzip_stream.Write(buffer, 0, buffer.Length);
bytes = memory_stream.ToArray();
}
int total_read = 0;
using (var input_stream = new MemoryStream(bytes))
using (var gzip_stream = new GZipStream(input_stream, CompressionMode.Decompress, true))
{
int read;
var buffer = new byte[4096];
while ((read = gzip_stream.Read(buffer, 0, buffer.Length)) != 0) {
total_read += read;
}
}
Debug.WriteLine(bytes);
Debug.WriteLine(total_read);
The gzipStr is a valid Gzipped Stream (I could compress it successfully with GzipStream() Compress).
Why is total_read always 0??? is gzip stream decompressing my stream? am I doing something wrong?
What am I doing wrong here???!!!
Upvotes: 1
Views: 1029
Reputation: 284786
You forgot to flush. :) Note that Encoding.Default should not generally be used in production. In the below, replace it with Encoding.UTF8 (or whatever's appropriate). Finally, of course, the below santiy-check only works if everything fits in a single buffer. But now you should get the idea.
kementeus indicated my previous code here didn't help, so below is the exact code I used:
public class GzipBug
{
public static void Main(String[] a)
{
byte[] bytes;
byte[] buffer;
Encoding encoding = Encoding.UTF8;
using (var memory_stream = new MemoryStream())
using (var gzip_stream = new GZipStream(memory_stream, CompressionMode.Compress))
{
buffer = encoding.GetBytes("Hello nurse!");
gzip_stream.Write(buffer, 0, buffer.Length);
gzip_stream.Flush();
bytes = memory_stream.ToArray();
}
int total_read = 0;
using (var input_stream = new MemoryStream(bytes))
using (var gzip_stream = new GZipStream(input_stream, CompressionMode.Decompress, true))
{
int read;
buffer = new byte[4096];
while ((read = gzip_stream.Read(buffer, 0, buffer.Length)) != 0) {
total_read += read;
}
}
Debug.WriteLine(encoding.GetString(buffer, 0, total_read));
Debug.WriteLine(total_read);
}
}
It is compiled with: gmcs -d:DEBUG -langversion:linq -debug+ GzipBug.cs and run as: MONO_TRACE_LISTENER=Console.Out GzipBug.exe
(you can remove the MONO_TRACE_LISTENER bit)
Upvotes: 2