0

Following various samples I've been able to convert a memory stream to a compressed stream and then to a byte array to save in a database but I'm having trouble going the other way. Here's what I've got so far...

...
using (MemoryStream compressedStream = new MemoryStream()) {
    ...some code that builds the compressedStream for an undetermined
    number of byteArrays from a database
    using (MemoryStream uncompressedStream = new MemoryStream()) {
        // method 1
        using (GZipStream unzippedStream = new GZipStream(compressedStream, CompressionMode.Decompress)) {
            unzippedStream.CopyTo(uncompressedStream);
        }
        // method 2
        using (GZipStream unzippedStream = new GZipStream(uncompressedStream, CompressionMode.Decompress)) {
            compressedStream.CopyTo(unzippedStream);
        }
        ... do something with uncompressedStream
    }
}

Method 1 seams to follows the examples I see on here but causes an error "stream does not support writing"

Method 2 seams to make more sense but the uncompressed stream is always empty

P.S. Really what I would like to have is something simple like

MemoryStream compressed = GZipStream(uncompressed, Compress)
MemoryStream upcompressed = GZipStream(compressed, Decompress)
nuander
  • 1,319
  • 1
  • 19
  • 33
  • Write to compress, read to decompress. – Bryce Wagner Jul 25 '16 at 22:57
  • In both cases, the underlying stream for the GZipStream contains the compressed data. – Bryce Wagner Jul 25 '16 at 22:59
  • Your first method should work. Are you sure that the byte array you use are the original compressed one? You can't just put compressed byte arrays together and expect it to decompiled you know. – Visual Vincent Jul 25 '16 at 23:11
  • Without a good [mcve] it's impossible to know for sure what you've done wrong. The short version of "how to" is: create the `MemoryStream` from your `byte[]`, pass that stream to `GZipStream`, and then _read_ from the `GZipStream`. – Peter Duniho Jul 25 '16 at 23:39
  • Related : http://stackoverflow.com/questions/3722192/how-do-i-use-gzipstream-with-system-io-memorystream –  Jul 25 '16 at 23:54
  • @Bruce - I'm not explicitly telling it to write anything, but at some point the decompressed stream has to be written somewhere in order to be useful. Can you elaborate please? – nuander Jul 26 '16 at 13:51
  • @Visual - the compressed data was converted to bytes and broken into chunks. I reassemble the chunks into a the compressed stream – nuander Jul 26 '16 at 13:56
  • 1
    Good, then if you have it in the right order your first method should not be a problem. – Visual Vincent Jul 26 '16 at 14:46
  • Yes it works now once I set the compressed stream position to 0. I'll attach a working code example – nuander Jul 26 '16 at 15:26

1 Answers1

0

This code example works. The first part is just to get a compressed byte array. The second part demonstrates how the compressed stream can be created in code, write can be done multiple times. But the position must be set to 0.

byte[] compressed;
string output;

using (var outStream = new MemoryStream()) {
    using (var tinyStream = new GZipStream(outStream, CompressionMode.Compress))
    using (var mStream = new MemoryStream(Encoding.UTF8.GetBytes("This is a test"))) {
        mStream.CopyTo(tinyStream);
    }
    compressed = outStream.ToArray();
}

using (var compressedStream = new MemoryStream()) {
    // can do multiple writes here to create the compressed stream
    compressedStream.Write(compressed, 0, compressed.Length);
    compressedStream.Flush();
    compressedStream.Position = 0;
    using (var unzippedStream = new GZipStream(compressedStream, CompressionMode.Decompress))
    using (var uncompressedStream = new MemoryStream()) {
        unzippedStream.CopyTo(uncompressedStream);
        output = Encoding.UTF8.GetString(uncompressedStream.ToArray());
    }
}

Console.WriteLine(output);
nuander
  • 1,319
  • 1
  • 19
  • 33