DeflateStream.Write DeflateStream.Write DeflateStream.Write DeflateStream.Write Method

Définition

Surcharges

Write(ReadOnlySpan<Byte>) Write(ReadOnlySpan<Byte>) Write(ReadOnlySpan<Byte>) Write(ReadOnlySpan<Byte>)

Écrit une séquence d’octets dans le flux Deflate actuel et avance la position actuelle dans ce flux Deflate du nombre d’octets écrits.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) Write(Byte[], Int32, Int32) Write(Byte[], Int32, Int32) Write(Byte[], Int32, Int32)

Écrit les octets compressés dans le flux sous-jacent à partir du tableau d'octets spécifié.Writes compressed bytes to the underlying stream from the specified byte array.

Write(ReadOnlySpan<Byte>) Write(ReadOnlySpan<Byte>) Write(ReadOnlySpan<Byte>) Write(ReadOnlySpan<Byte>)

Écrit une séquence d’octets dans le flux Deflate actuel et avance la position actuelle dans ce flux Deflate du nombre d’octets écrits.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))

Paramètres

buffer
ReadOnlySpan<Byte>

Région de la mémoire.A region of memory. Cette méthode copie le contenu de cette région dans le flux Deflate actuel.This method copies the contents of this region to the current Deflate stream.

Remarques

Utilisez la CanWrite propriété pour déterminer si l’instance actuelle prend en charge l’écriture.Use the CanWrite property to determine whether the current instance supports writing. Utilisez la WriteAsync méthode pour écrire de façon asynchrone dans le flux actuel.Use the WriteAsync method to write asynchronously to the current stream.

Si l’opération d’écriture réussit, la position dans le flux de déflateur avance du nombre d’octets écrits.If the write operation is successful, the position within the Deflate stream advances by the number of bytes written. Si une exception se produit, la position dans le flux deflate reste inchangée.If an exception occurs, the position within the Deflate stream remains unchanged.

Write(Byte[], Int32, Int32) Write(Byte[], Int32, Int32) Write(Byte[], Int32, Int32) Write(Byte[], Int32, Int32)

Écrit les octets compressés dans le flux sous-jacent à partir du tableau d'octets spécifié.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)

Paramètres

array
Byte[]

Mémoire tampon qui contient les données à compresser.The buffer that contains the data to compress.

offset
Int32 Int32 Int32 Int32

Dans array, décalage d'octet à partir duquel les octets seront lus.The byte offset in array from which the bytes will be read.

count
Int32 Int32 Int32 Int32

Nombre maximal d'octets à écrire.The maximum number of bytes to write.

Exemples

L’exemple suivant montre comment compresser et décompresser les octets Read à Write l’aide des méthodes et.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

S’applique à