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???!!!
-
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)
kementeus : Ok, I got your point with gzip stream Flush and use Encoding.Default / Encoding.UTF8... But my issue was never related to the gzip compression, it works fine... I did your modifications but total read _stills_ 0 So the main issue persistsMatthew Flaschen : I just posted the exact code I'm using. If you take out the Flush, it gives 0, if you put it in, it gives 12. I agree the Encoding.UTF8 is a side issue, but it's always worth paying attention to encoding.
0 comments:
Post a Comment