Copy between two streams in .net 2.0

2019-05-02 02:43发布

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

}

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'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

7条回答
我命由我不由天
2楼-- · 2019-05-02 03:10

The open-source NuGet package Stream.CopyTo implements Stream.CopyTo for all versions of the .NET Framework.

Available on GitHub and via NuGet (Install-Package Stream.CopyTo)

查看更多
贪生不怕死
3楼-- · 2019-05-02 03:10

This is the code straight out of .Net 4.0 Stream.CopyTo method (bufferSize is 4096):

byte[] buffer = new byte[bufferSize];
int count;
while ((count = this.Read(buffer, 0, buffer.Length)) != 0)
    destination.Write(buffer, 0, count);
查看更多
倾城 Initia
4楼-- · 2019-05-02 03:15

try to replace the line:

Compress.Write(inFile.GetBuffer(), (int)inFile.Position, (int)(inFile.Length - inFile.Position));

with:

Compress.Write(data_toCompress, 0, data_toCompress.Length);

you can get rid of this line completely:

using (MemoryStream inFile = new MemoryStream(data_toCompress))

Edit: find an example here: Why does gzip/deflate compressing a small file result in many trailing zeroes?

查看更多
Viruses.
5楼-- · 2019-05-02 03:19

You should manually read and write between these 2 streams:

    private static void CopyStream(Stream from, Stream to)
    {
        int bufSize = 1024, count;
        byte[] buffer = new byte[bufSize];
        count = from.Read(buffer, 0, bufSize);
        while (count > 0)
        {
            to.Write(buffer, 0, count);
            count = from.Read(buffer, 0, bufSize);
        }
    }
查看更多
我想做一个坏孩纸
6楼-- · 2019-05-02 03:21

You can try

infile.WriteTo(Compress);
查看更多
Melony?
7楼-- · 2019-05-02 03:23

Why are you constructing a memory stream with an array and then trying to pull the array back out of the memory stream?

You could just do Compress.Write(data_toCompress, 0, data_toCompress.Length);

If you need to replace the functionality of CopyTo, you can create a buffer array of some length, read data from the source stream and write that data to the destination stream.

查看更多
登录 后发表回答