Search code examples
c#.netmonogzipgzipstream

Using .NET GZipStream Class with Mono


I'm trying to build an example from GZipStream Class. With the command gmcs gzip.cs, I got error messages. gzip.cs is the same source from the msdn.

It seems that I need to add reference when compiling. What's missing?

gzip.cs(57,32): error CS1061: Type `System.IO.FileStream' does not contain a definition for `CopyTo' and no extension method `CopyTo' of type `System.IO.FileStream' could be found (are you missing a using directive or an assembly reference?)
/Library/Frameworks/Mono.framework/Versions/2.10.1/lib/mono/2.0/mscorlib.dll (Location of the symbol related to previous error)
gzip.cs(86,40): error CS1061: Type `System.IO.Compression.GZipStream' does not contain a definition for `CopyTo' and no extension method `CopyTo' of type `System.IO.Compression.GZipStream' could be found (are you missing a using directive or an assembly reference?)
/Library/Frameworks/Mono.framework/Versions/2.10.1/lib/mono/gac/System/2.0.0.0__b77a5c561934e089/System.dll (Location of the symbol related to previous error)
Compilation failed: 2 error(s), 0 warnings

SOLVED

I should use 'dmcs', not 'gmcs' in order to use .NET 4 functions.


Solution

  • Stream.CopyTo only arrived in .NET 4 - it may not be in Mono yet (or perhaps you need a more recent version).

    It's easy to write a similar extension method though:

    public static class StreamExtensions
    {
        public static void CopyTo(this Stream input, Stream output)
        {
            byte[] buffer = new byte[16 * 1024];
            int bytesRead;
            while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
            {
                output.Write(buffer, 0, bytesRead);
            }
        }
    }