DeflateStream.Write 方法

定义

重载

Write(ReadOnlySpan<Byte>)

将字节序列写入当前 Deflate 流,并按写入的字节数将 Deflate 流内的当前位置向前移动。Writes a sequence of bytes to the current Deflate stream and advances the current position within this Deflate stream by the number of bytes written.

Write(Byte[], Int32, Int32)

从指定的字节数组中将压缩的字节写入基础流。Writes compressed bytes to the underlying stream from the specified byte array.

Write(ReadOnlySpan<Byte>)

将字节序列写入当前 Deflate 流,并按写入的字节数将 Deflate 流内的当前位置向前移动。Writes a sequence of bytes to the current Deflate stream and advances the current position within this Deflate stream by the number of bytes written.

public:
 override void Write(ReadOnlySpan<System::Byte> buffer);
public override void Write (ReadOnlySpan<byte> buffer);
override this.Write : ReadOnlySpan<byte> -> unit
Public Overrides Sub Write (buffer As ReadOnlySpan(Of Byte))

参数

buffer
ReadOnlySpan<Byte>

内存的区域。A region of memory. 此方法将此区域的内容复制到当前 Deflate 流。This method copies the contents of this region to the current Deflate stream.

注解

使用 CanWrite 属性可确定当前实例是否支持写入。Use the CanWrite property to determine whether the current instance supports writing. 使用 WriteAsync 方法异步写入当前流。Use the WriteAsync method to write asynchronously to the current stream.

如果写入操作成功,Deflate 流中的位置将按写入的字节数向前移动。If the write operation is successful, the position within the Deflate stream advances by the number of bytes written. 如果发生异常,则 Deflate 流中的位置将保持不变。If an exception occurs, the position within the Deflate stream remains unchanged.

Write(Byte[], Int32, Int32)

从指定的字节数组中将压缩的字节写入基础流。Writes compressed bytes to the underlying stream from the specified byte array.

public:
 override void Write(cli::array <System::Byte> ^ array, int offset, int count);
public override void Write (byte[] array, int offset, int count);
override this.Write : byte[] * int * int -> unit
Public Overrides Sub Write (array As Byte(), offset As Integer, count As Integer)

参数

array
Byte[]

包含要压缩的数据的缓冲区。The buffer that contains the data to compress.

offset
Int32

array 中的字节偏移量,将在此处读取字节。The byte offset in array from which the bytes will be read.

count
Int32

最多写入的字节数。The maximum number of bytes to write.

示例

下面的示例演示如何使用 ReadWrite 方法压缩和解压缩字节。The following example shows how to compress and decompress bytes by using the Read and Write methods.

using System;
using System.Text;
using System.IO;
using System.IO.Compression;

namespace ExampleConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            UnicodeEncoding uniEncode = new UnicodeEncoding();

            byte[] bytesToCompress = uniEncode.GetBytes("example text to compress and decompress");
            Console.WriteLine("starting with: " + uniEncode.GetString(bytesToCompress));

            using (FileStream fileToCompress = File.Create("examplefile.gz"))
            {
                using (DeflateStream compressionStream = new DeflateStream(fileToCompress, CompressionMode.Compress))
                {
                    compressionStream.Write(bytesToCompress, 0, bytesToCompress.Length);
                }
            }

            byte[] decompressedBytes = new byte[bytesToCompress.Length];
            using (FileStream fileToDecompress = File.Open("examplefile.gz", FileMode.Open))
            {
                using (DeflateStream decompressionStream = new DeflateStream(fileToDecompress, CompressionMode.Decompress))
                {
                    decompressionStream.Read(decompressedBytes, 0, bytesToCompress.Length);
                }
            }
            
            Console.WriteLine("ending with: " + uniEncode.GetString(decompressedBytes));
        }
    }
}
Imports System.IO
Imports System.IO.Compression
Imports System.Text

Module Module1

    Sub Main()
        Dim uniEncode As UnicodeEncoding = New UnicodeEncoding()

        Dim bytesToCompress = uniEncode.GetBytes("example text to compress and decompress")
        Console.WriteLine("starting with: " + uniEncode.GetString(bytesToCompress))

        Using fileToCompress As FileStream = File.Create("examplefile.gz")
            Using compressionStream As DeflateStream = New DeflateStream(fileToCompress, CompressionMode.Compress)
                compressionStream.Write(bytesToCompress, 0, bytesToCompress.Length)
            End Using
        End Using

        Dim decompressedBytes(bytesToCompress.Length - 1) As Byte
        Using fileToDecompress As FileStream = File.Open("examplefile.gz", FileMode.Open)
            Using decompressionStream As DeflateStream = New DeflateStream(fileToDecompress, CompressionMode.Decompress)
                decompressionStream.Read(decompressedBytes, 0, bytesToCompress.Length)
            End Using
        End Using

        Console.WriteLine("ending with: " + uniEncode.GetString(decompressedBytes))
    End Sub
End Module

适用于