views:

108

answers:

1

Hi

I dont get it. I have used a similar/same approach for many years now, and never experienced this.

For some reason, that I did not pick up until today, a GZip round trip results in 1 or more bytes being truncated or data being garbled.

I wrote a simple test to verify that something else is not affecting it.

This always fails with a 'length mismatch'.

Can someone conform to me that I am not crazy? :)

Thanks

leppie

TEST

using System;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;

class Program
{
  const int BUFFER_SIZE = 8192;

  static void Main(string[] args)
  {
    var filename = args[0];
    var filedata = File.ReadAllBytes(filename);
    var cmp = Compress(filedata);
    var dec = Decompress(cmp);

    Assert(filedata, dec);

    Console.ReadLine();
  }

  static void Assert(byte[] orig, byte[] data)
  {
    if (orig.Length != data.Length)
    {
      Debug.Fail("length mismatch");
    }
    for (int i = 0; i < orig.Length; i++)
    {
      Debug.Assert(orig[i] == data[i], "data mismatch");
    }
  }

  static byte[] Compress(byte[] data)
  {
    var input = new MemoryStream(data);
    var output = new MemoryStream();

    var s = new GZipStream(output, CompressionMode.Compress);
    byte[] buffer = new byte[BUFFER_SIZE];
    int read = 0;
    while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
    {
      s.Write(buffer, 0, read);
    }

    return output.ToArray();
  }

  static byte[] Decompress(byte[] data)
  {
    var input = new MemoryStream(data);
    var s = new GZipStream(input, CompressionMode.Decompress);

    var output = new MemoryStream();
    byte[] buffer = new byte[BUFFER_SIZE];
    int read = 0;
    while ((read = s.Read(buffer, 0, buffer.Length)) > 0)
    {
      output.Write(buffer, 0, read);
    }

    return output.ToArray();
  }
}

I have tried it with closing the streams properly too, with different buffer sizes, all the same result.

A: 

OK, found the problem.

You need to close the compression stream before retrieving the bytes.

Eg:

s.Close();
return output.ToArray();
leppie