Decompress result in a Memory Stream using ICSharpDevelop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fsores
    New Member
    • Oct 2008
    • 1

    Decompress result in a Memory Stream using ICSharpDevelop

    Hi,

    I'm trying to decompress some data throgug ICSharpDevelop, but can't get it to work.

    Here is my code:

    private static string UnGZip(byte[] Dado)
    {
    MemoryStream outputMemStream = new MemoryStream(Da do);
    MemoryStream PlainStream = new MemoryStream();
    using (ICSharpCode.Sh arpZipLib.GZip. GZipInputStream zipInput = new ICSharpCode.Sha rpZipLib.GZip.G ZipInputStream( outputMemStream )) {

    byte[] buf = new byte[16384];
    int l = -1;
    while ((l = zipInput.Read(b uf, 0, buf.Length)) != 0)
    PlainStream.Wri te(buf, 0, l);

    return Encoding.ASCII. GetString(Plain Stream.ToArray( ));
    }


    }

    I keep getting the error EOF something.

    I was using .NET library before, but wasn't working in some machines.

    Thanks
    Felipe Soares
  • mldisibio
    Recognized Expert New Member
    • Sep 2008
    • 191

    #2
    I cannot help you with a specific third-party library. But converting your code 'as-is' to using the .Net GZipStream, it works fine. The problem might be with the incoming byte array.

    Make sure your incoming byte array was compressed with GZip and not WinZip.
    Make sure the method writing compressed bytes closes the GZipStream first so that the final compression bits are flushed correctly.

    I noticed in your code you leave both your MemoryStreams open. If the method creating the byte array is as equally sloppy, you are opening your code to possibilites of unnecessary IO exceptions.

    Your code using standard libraries: works fine:
    Code:
        private static string UnGZip(byte[] Dado) {
          MemoryStream outputMemStream = new MemoryStream(Dado);
          MemoryStream PlainStream = new MemoryStream();
          using (GZipStream zipInput = new GZipStream(outputMemStream, CompressionMode.Decompress, true)) {
    
            byte[] buf = new byte[16384];
            int l = -1;
            while ((l = zipInput.Read(buf, 0, buf.Length)) != 0)
              PlainStream.Write(buf, 0, l);
    
            return Encoding.ASCII.GetString(PlainStream.ToArray());
          }
        }
    Make sure you close all IO streams correctly.
    Code:
        public static string DecompressGZipBytes(byte[] compressedBytes) {
          int BUFFER_SIZE = 16384;
          int bytesRead = -1;
          byte[] buffer = new byte[BUFFER_SIZE];
          using (MemoryStream inputWrapper = new MemoryStream(compressedBytes)) {
            using (MemoryStream decompressedOutput = new MemoryStream()) {
              using (GZipStream zipInput = new GZipStream(inputWrapper, CompressionMode.Decompress, true)) {
                while ((bytesRead = zipInput.Read(buffer, 0, BUFFER_SIZE)) != 0)
                  decompressedOutput.Write(buffer, 0, bytesRead);
              } // close zip stream
              return Encoding.ASCII.GetString(decompressedOutput.ToArray());
            } // close output stream
          } // close input stream
        }

    Comment

    Working...