在.NET 2.0两股之间复制 [英] Copy between two streams in .net 2.0

查看:96
本文介绍了在.NET 2.0两股之间复制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用下面的代码在.NET 4.0中对数据进行压缩:

I have been using the following code to Compress data in .Net 4.0:

public static byte[] CompressData(byte[] data_toCompress)
{

    using (MemoryStream outFile = new MemoryStream())
    {
        using (MemoryStream inFile = new MemoryStream(data_toCompress))
        using (GZipStream Compress = new GZipStream(outFile, CompressionMode.Compress))
        {
            inFile.CopyTo(Compress);

        }
        return outFile.ToArray();
    }

}

然而,在NET 2.0流.CopyTo方法不可用。所以,我试图使替代:

However, in .Net 2.0 Stream.CopyTo method is not available. So, I tried making a replacement:

public static byte[] CompressData(byte[] data_toCompress)
{

    using (MemoryStream outFile = new MemoryStream())
    {
        using (MemoryStream inFile = new MemoryStream(data_toCompress))
        using (GZipStream Compress = new GZipStream(outFile, CompressionMode.Compress))
        {
            //inFile.CopyTo(Compress);
            Compress.Write(inFile.GetBuffer(), (int)inFile.Position, (int)(inFile.Length - inFile.Position));
        }
        return outFile.ToArray();
    }

}



压缩失败,不过,当用上面的尝试 - 我得到一个错误说:

The compression fails, though, when using the above attempt - I get an error saying:

的MemoryStream的内部缓冲区无法访问

MemoryStream's internal buffer cannot be accessed.

任何人都可以提供关于这个问题的任何帮助吗?我真的不知道还有什么需要做。

Could anyone offer any help on this issue? I'm really not sure what else to do here.

感谢您,
埃文

Thank you, Evan

推荐答案

既然你有访问阵列已经,你为什么不这样做:

Since you have access to the array already, why don't you do this:

using (MemoryStream outFile = new MemoryStream())
{
    using (GZipStream Compress = new GZipStream(outFile, CompressionMode.Compress))
    {
        Compress.Write(data_toCompress, 0, data_toCompress.Length);
    }
    return outFile.ToArray();
}



最有可能在你使用的示例代码 INFILE .GetBuffer()将抛出一个异常,因为你不使用的右键构造 - 不是所有的的MemoryStream 实例允许您访问到内部缓冲区 - 你要寻找这个在文档:

Most likely in the sample code you are using inFile.GetBuffer() will throw an exception since you do not use the right constructor - not all MemoryStream instances allow you access to the internal buffer - you have to look for this in the documentation:

初始化基于指定的字节数组区域
将MemoryStream类的新实例,并用CanWrite属性设置为指定
并呼吁为指定

Initializes a new instance of the MemoryStream class based on the specified region of a byte array, with the CanWrite property set as specified, and the ability to call GetBuffer set as specified.

这应该的GetBuffer设置的能力工作 - 但不是在建议的解决方案无论如何需要:

This should work - but is not needed anyway in the suggested solution:

using (MemoryStream inFile = new MemoryStream(data_toCompress, 
                                              0, 
                                              data_toCompress.Length, 
                                              false, 
                                              true))

这篇关于在.NET 2.0两股之间复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆