DeflateStream.Write Metodo

Definizione

Overload

Write(ReadOnlySpan<Byte>)

Scrive una sequenza di byte nel flusso Deflate corrente e passa alla posizione successiva nel flusso Deflate in base al numero di byte scritti.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)

Scrive i byte compressi nel flusso sottostante dalla matrice di byte specificata.Writes compressed bytes to the underlying stream from the specified byte array.

Write(ReadOnlySpan<Byte>)

Scrive una sequenza di byte nel flusso Deflate corrente e passa alla posizione successiva nel flusso Deflate in base al numero di byte scritti.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))

Parametri

buffer
ReadOnlySpan<Byte>

Un'area di memoria.A region of memory. Questo metodo copia il contenuto di questa area nel flusso Deflate corrente.This method copies the contents of this region to the current Deflate stream.

Commenti

Utilizzare la CanWrite proprietà per determinare se l'istanza corrente supporta la scrittura.Use the CanWrite property to determine whether the current instance supports writing. Usare il WriteAsync metodo per scrivere in modo asincrono nel flusso corrente.Use the WriteAsync method to write asynchronously to the current stream.

Se l'operazione di scrittura ha esito positivo, la posizione all'interno del flusso deflate avanza per il numero di byte scritti.If the write operation is successful, the position within the Deflate stream advances by the number of bytes written. Se si verifica un'eccezione, la posizione all'interno del flusso deflate rimane invariata.If an exception occurs, the position within the Deflate stream remains unchanged.

Si applica a

Write(Byte[], Int32, Int32)

Scrive i byte compressi nel flusso sottostante dalla matrice di byte specificata.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)

Parametri

array
Byte[]

Buffer contenente i dati da comprimere.The buffer that contains the data to compress.

offset
Int32

Offset dei byte in array da cui verranno letti i byte.The byte offset in array from which the bytes will be read.

count
Int32

Numero massimo di byte da scrivere.The maximum number of bytes to write.

Esempio

Nell'esempio seguente viene illustrato come comprimere e decomprimere byte utilizzando i Read Write metodi e.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

Si applica a