tags:

views:

11

answers:

1

hi in this code in .net 4 i used copyto method of gzipstream

        System.IO.MemoryStream ms = new System.IO.MemoryStream(byteArray);
        GZipStream DecompressOut = new GZipStream(ms, System.IO.Compression.CompressionMode.Decompress);
        MemoryStream outmem = new MemoryStream();
        DecompressOut.copyto(outmem);
        FileStream outFile = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
        StreamWriter m_streamWriter = new StreamWriter(outFile);

how can i diretly write GZipStream into MemoryStream or FileStream?

+1  A: 

Copying between streams is pretty basic:

public static long CopyTo(this Stream source, Stream destination) {
    byte[] buffer = new byte[2048];
    int bytesRead;
    long totalBytes = 0;
    while((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0) {
        destination.Write(buffer, 0, bytesRead);
        totalBytes += bytesRead;
    }
    return totalBytes;
}

So just plug that in, and you should be sorted:

using(var ms = new MemoryStream(byteArray))
using(var gzip = new GZipStream(ms, CompressionMode.Decompress))
using (var file = new FileStream(fileName, FileMode.OpenOrCreate,
                                           FileAccess.Write)) {
    gzip.CopyTo(file);
}
Marc Gravell