DeflateStream.Write 方法

定义

重载

Write(ReadOnlySpan<Byte>)

将字节序列写入当前 Deflate 流,并按写入的字节数将 Deflate 流内的当前位置向前移动。

Write(Byte[], Int32, Int32)

从指定的字节数组中将压缩的字节写入基础流。

Write(ReadOnlySpan<Byte>)

Source:
DeflateStream.cs
Source:
DeflateStream.cs
Source:
DeflateStream.cs

将字节序列写入当前 Deflate 流,并按写入的字节数将 Deflate 流内的当前位置向前移动。

C#
public override void Write (ReadOnlySpan<byte> buffer);

参数

buffer
ReadOnlySpan<Byte>

内存的区域。 此方法将此区域的内容复制到当前 Deflate 流。

注解

CanWrite使用 属性确定当前实例是否支持写入。 WriteAsync使用 方法以异步方式写入当前流。

如果写入操作成功,Deflate 流中的位置将按写入的字节数前进。 如果发生异常,Deflate 流中的位置保持不变。

适用于

.NET 9 和其他版本
产品 版本
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Standard 2.1

Write(Byte[], Int32, Int32)

Source:
DeflateStream.cs
Source:
DeflateStream.cs
Source:
DeflateStream.cs

从指定的字节数组中将压缩的字节写入基础流。

C#
public override void Write (byte[] array, int offset, int count);
C#
public override void Write (byte[] buffer, int offset, int count);

参数

arraybuffer
Byte[]

包含要压缩的数据的缓冲区。

offset
Int32

将从中读取字节的字节偏移量。

count
Int32

最多写入的字节数。

注解

以下示例演示如何使用 ReadWrite 方法压缩和解压缩字节。

C#
using System;
using System.IO;
using System.IO.Compression;
using System.Text;

public static class MemoryWriteReadExample
{
    private const string Message = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
    private static readonly byte[] s_messageBytes = Encoding.ASCII.GetBytes(Message);

    public static void Run()
    {
        Console.WriteLine($"The original string length is {s_messageBytes.Length} bytes.");
        using var stream = new MemoryStream();
        CompressBytesToStream(stream);
        Console.WriteLine($"The compressed stream length is {stream.Length} bytes.");
        int decompressedLength = DecompressStreamToBytes(stream);
        Console.WriteLine($"The decompressed string length is {decompressedLength} bytes, same as the original length.");
        /*
         Output:
            The original string length is 445 bytes.
            The compressed stream length is 265 bytes.
            The decompressed string length is 445 bytes, same as the original length.
        */
    }

    private static void CompressBytesToStream(Stream stream)
    {
        using var compressor = new DeflateStream(stream, CompressionMode.Compress, leaveOpen: true);
        compressor.Write(s_messageBytes, 0, s_messageBytes.Length);
    }

    private static int DecompressStreamToBytes(Stream stream)
    {
        stream.Position = 0;
        int bufferSize = 512;
        byte[] buffer = new byte[bufferSize];
        using var deflateStream = new DeflateStream(stream, CompressionMode.Decompress);

        int totalRead = 0;
        while (totalRead < buffer.Length)
        {
            int bytesRead = deflateStream.Read(buffer.AsSpan(totalRead));
            if (bytesRead == 0) break;
            totalRead += bytesRead;
        }

        return totalRead;
    }
}

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0