将 .NET GZipStream Class 与 Mono 一起使用

本文关键字:一起 Mono NET GZipStream Class | 更新日期: 2023-09-27 17:56:00

我正在尝试从GZipStream Class构建一个示例。使用命令gmcs gzip.cs,我收到错误消息。gzip.cs 是来自 MSDN 的同一源。

看来编译时需要添加参考。缺少什么?

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

解决

我应该使用"dmcs",而不是"gmcs"才能使用.NET 4函数。

将 .NET GZipStream Class 与 Mono 一起使用

Stream.CopyTo只出现在.NET 4中 - 它可能还没有在Mono中(或者可能需要更新的版本)。

不过,编写类似的扩展方法很容易:

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);
        }
    }
}