[Mono-dev] Status of System.IO.Compression (both Deflate and Gzip)

Jonathan Gagnon jonathan.gagnon at croesus.com
Tue Jul 24 07:47:42 EDT 2007


Hi,

I noticed the same problem.  There seem to be a problem when reading more
than once the same compressed stream.  If possible, try reading everything
in one call (you need to know the actual size of the stream once
decompressed).  This works for me.

-----Message d'origine-----
De : mono-devel-list-bounces at lists.ximian.com
[mailto:mono-devel-list-bounces at lists.ximian.com] De la part de David
Wolinsky
Envoyé : Monday, July 23, 2007 4:14 PM
À : mono-devel
Objet : [Mono-dev] Status of System.IO.Compression (both Deflate and Gzip)

I am not having luck with either class using 1.2.4, they both compile but
the results never come out right... perhaps they aren't fully implemented
yet?

Here's what I'm using...

using System;
using System.IO.Compression;
using System.IO;
using System.Security.Cryptography;

namespace poop {
  public class poopie {
    public static void Main() {
      byte [] data = new byte[100000];
      for (int i = 0; i < 100000; i++) {
        data[i] = (byte) i;
      }
      MemoryStream dataStream = new MemoryStream (data);
      MemoryStream backing = new MemoryStream ();
      GZipStream compressing = new GZipStream (backing,
CompressionMode.Compress, true);
      CopyStream (dataStream, compressing);
      dataStream.Close();
// This next method causes an exception and if uncommented still it doesn't
work right...
      compressing.Close();
      backing.Seek (0, SeekOrigin.Begin);
      GZipStream decompressing = new GZipStream (backing,
CompressionMode.Decompress);
      MemoryStream output = new MemoryStream ();
      CopyStream (decompressing, output);
      Console.WriteLine(backing.Length);
      for (int i = 0; i < backing.Length; i++) {
        Console.WriteLine(backing.GetBuffer()[i]);
      }
      Console.WriteLine(compare_buffers (data, output.GetBuffer(), (int)
data.Length));
      decompressing.Close();
      output.Close();
    }

    private static void CopyStream (Stream src, Stream dest)
    {
      byte[] array = new byte[1024];
      int bytes_read;
      bytes_read = src.Read (array, 0, 1024);
      while (bytes_read != 0) {
        dest.Write (array, 0, bytes_read);
        bytes_read = src.Read (array, 0, 1024);
      }
    }

    private static bool compare_buffers (byte[] first, byte[] second, int
length)
    {
      if (first.Length < length || second.Length < length) {
        return false;
      }
      for (int i = 0; i < length; i++) {
        if (first[i] != second[i]) {
          return false;
        }
      }
      return true;
    }
  }
}
_______________________________________________
Mono-devel-list mailing list
Mono-devel-list at lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-devel-list




More information about the Mono-devel-list mailing list