0

How to write binary data from GZipStream after decompression to a file as binary data in C# .NET 2.0:

FileStream fileStream = new FileStream("compressed_file.zip", FileMode.Open, FileAccess.Read);
GZipStream compressionStream = new GZipStream(fileStream, CompressionMode.Decompress);

//Now how to save uncompressed binary data in "compressionStream" as a file ???
Computer User
  • 2,839
  • 4
  • 47
  • 69

1 Answers1

2

its a two line solution, you just need to copy the compressionstream to a filestream.

using (FileStream outFile = new FileStream(sOutFilePath, FileMode.Create, FileAccess.Write, FileShare.None))
                  CopyStream(compressionStream, outFile);

Ok you need to make sure you are at the beginning of the compressionStream so adding

compressionStream.Seek(0, SeekOrigin.Begin);

could be necessary.

EDIT:

Sorry so the implementation of copyTo should be something like this:

public void CopyStream(Stream source, Stream destination, int bufferSize = 81920)
{
    byte[] buffer = new byte[bufferSize];
    int read;
    while ((read = source.Read(buffer, 0, buffer.Length)) != 0)
        destination.Write(buffer, 0, read);
}

EDIT2:

the default bufferSize in the .Net4.5 implementation is defined as follows:

private const int _DefaultCopyBufferSize = 81920;

Full Source at referencesource.microsoft.com

BoeseB
  • 695
  • 4
  • 17