[Mono-list] does GZipStream class has any bugs?

CarGa carga at mail.ru
Tue Nov 25 04:41:19 EST 2008


Hello, all.

I tried zip/unzip actions with GZipStream class and had no luck with it. It looks like zip process works well, but unzip fails completely.

Here is some snippets here:

Zipping method seems to work well

protected static byte [] GetDefaultSerializedForm (byte [] bytesToZip) {
	byte [] buffer = null;
	using (MemoryStream ms2 = new MemoryStream ()) {
		using (var gzip = new GZipStream (ms2, CompressionMode.Compress, true)) {
			gzip.Write (bytesToZip, 0, bytesToZip.Length);
			gzip.Flush ();
			ms2.Position = 0;
			buffer = ms2.ToArray ();
			gzip.Close ();
		}
		ms2.Close ();
	}

	return buffer;
}

But unzip...

protected static byte[] RestoreFromDefaultSerializedForm (byte [] bytesToUnzip) {
	byte [] result = null;
	byte [] buffer = new byte [8192];
	using(MemoryStream unpacked = new MemoryStream())
	using (MemoryStream ms = new MemoryStream (bytesToUnzip)) {
	    ms.Position = 0;
	    using (GZipStream gunzip = new GZipStream (ms, CompressionMode.Decompress, true)) {
	        ms.Position = 0;
	        int count=0;
		while ((count = gunzip.Read (buffer, 0, buffer.Length)) > 0) {
	            unpacked.Write(buffer, 0, count);
	        }
	        gunzip.Close ();
	    }
	    ms.Close ();
	    unpacked.Position = 0;
	    result = unpacked.ToArray ();
	}

	return result;
}

Debugging shows, that it never comes to unpacked.Write(buffer, 0, count); line, because gunzip.Read(...) always returns 0. At the same time after gunzip.Read(...) ms.Position changes and becomes equal to the size of the bytesToUnzip array. Extremely weird!

Please, advice me how to implement decompression with GZipStream. Code snippet maybe...

Environment: Ubuntu 8.10, mono is compiled from svn (rev. 119107).

Thank you in advance,
Anton.


More information about the Mono-list mailing list