How to convert a compressed stream to an uncompressed stream in c# using GZipStream -


following various samples i've been able convert memory stream compressed stream , byte array save in database i'm having trouble going other way. here's i've got far...

... using (memorystream compressedstream = new memorystream()) {     ...some code builds compressedstream undetermined     number of bytearrays 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);         }         ... uncompressedstream     } } 

method 1 seams follows examples see on here causes error "stream not support writing"

method 2 seams make more sense uncompressed stream empty

p.s. have simple like

memorystream compressed = gzipstream(uncompressed, compress) memorystream upcompressed = gzipstream(compressed, decompress) 

this code example works. first part compressed byte array. second part demonstrates how compressed stream can created in code, write can done multiple times. position must set 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 test"))) {         mstream.copyto(tinystream);     }     compressed = outstream.toarray(); }  using (var compressedstream = new memorystream()) {     // can multiple writes here create 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); 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -