DeflateStream Constructeurs

Définition

Initialise une nouvelle instance de la classe DeflateStream.Initializes a new instance of the DeflateStream class.

Surcharges

DeflateStream(Stream, CompressionLevel)

Initialise une nouvelle instance de la classe DeflateStream à l'aide du flux et du niveau de compression spécifiés.Initializes a new instance of the DeflateStream class by using the specified stream and compression level.

DeflateStream(Stream, CompressionMode)

Initialise une nouvelle instance de la classe DeflateStream à l'aide du flux et du mode de compression spécifiés.Initializes a new instance of the DeflateStream class by using the specified stream and compression mode.

DeflateStream(Stream, CompressionLevel, Boolean)

Initialise une nouvelle instance de la classe DeflateStream à l'aide du flux et du niveau de compression spécifiés, et permet éventuellement de laisser le flux ouvert.Initializes a new instance of the DeflateStream class by using the specified stream and compression level, and optionally leaves the stream open.

DeflateStream(Stream, CompressionMode, Boolean)

Initialise une nouvelle instance de la classe DeflateStream à l'aide du flux et du mode de compression spécifiés, et permet éventuellement de laisser le flux ouvert.Initializes a new instance of the DeflateStream class by using the specified stream and compression mode, and optionally leaves the stream open.

Remarques

L’utilisation DeflateStream de la classe pour compresser un fichier d’une taille supérieure à 4 Go entraîne une exception.Using the DeflateStream class to compress a file larger than 4 GB will cause an exception.

DeflateStream(Stream, CompressionLevel)

Initialise une nouvelle instance de la classe DeflateStream à l'aide du flux et du niveau de compression spécifiés.Initializes a new instance of the DeflateStream class by using the specified stream and compression level.

public:
 DeflateStream(System::IO::Stream ^ stream, System::IO::Compression::CompressionLevel compressionLevel);
public DeflateStream (System.IO.Stream stream, System.IO.Compression.CompressionLevel compressionLevel);
new System.IO.Compression.DeflateStream : System.IO.Stream * System.IO.Compression.CompressionLevel -> System.IO.Compression.DeflateStream

Paramètres

stream
Stream

Flux à compresser.The stream to compress.

compressionLevel
CompressionLevel

L'une des valeurs d'énumération qui indique s'il faut mettre l'accent sur la rapidité et l'efficacité de compression en compressant le flux de données.One of the enumeration values that indicates whether to emphasize speed or compression efficiency when compressing the stream.

Exceptions

stream a la valeur null.stream is null.

Le flux ne prend pas en charge les opérations d’écriture telles que la compression.The stream does not support write operations such as compression. (La propriété CanWrite sur l’objet de flux est false.)(The CanWrite property on the stream object is false.)

Exemples

L’exemple suivant montre comment définir le niveau de compression lors de la DeflateStream création d’un objet.The following example shows how to set the compression level when creating a DeflateStream object.

using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string directoryPath = @"c:\users\public\reports";
            DirectoryInfo directorySelected = new DirectoryInfo(directoryPath);

            foreach (FileInfo fileToCompress in directorySelected.EnumerateFiles())
            {
                Compress(fileToCompress);
            }
        }

        public static void Compress(FileInfo fileToCompress)
        {
            using (FileStream originalFileStream = fileToCompress.OpenRead())
            {
                if ((File.GetAttributes(fileToCompress.FullName) & FileAttributes.Hidden) != FileAttributes.Hidden & fileToCompress.Extension != ".cmp")
                {
                    using (FileStream compressedFileStream = File.Create(fileToCompress.FullName + ".cmp"))
                    {
                        using (DeflateStream compressionStream = new DeflateStream(compressedFileStream, CompressionLevel.Fastest))
                        {
                            originalFileStream.CopyTo(compressionStream);
                        }
                    }
                }
            }
        }
    }
}
Imports System.IO
Imports System.IO.Compression

Module Module1

    Sub Main()
        Dim directoryPath As String = "c:\users\public\reports"
        Dim directorySelected As DirectoryInfo = New DirectoryInfo(directoryPath)

        For Each fileToCompress As FileInfo In directorySelected.EnumerateFiles()
            Compress(fileToCompress)
        Next
    End Sub

    Public Sub Compress(fileToCompress As FileInfo)
        Using originalFileStream As FileStream = fileToCompress.OpenRead()
            If ((File.GetAttributes(fileToCompress.FullName) & FileAttributes.Hidden) <> FileAttributes.Hidden And fileToCompress.Extension <> ".cmp") Then
                Using compressedFileStream As FileStream = File.Create(fileToCompress.FullName + ".cmp")
                    Using compressionStream As DeflateStream = New DeflateStream(compressedFileStream, CompressionLevel.Fastest)
                        originalFileStream.CopyTo(compressionStream)
                    End Using
                End Using
            End If
        End Using
    End Sub

End Module

Remarques

Vous utilisez ce constructeur lorsque vous souhaitez spécifier si l’efficacité de la compression ou la vitesse est plus importante pour une DeflateStream instance de la classe.You use this constructor when you want to specify whether compression efficiency or speed is more important for an instance of the DeflateStream class.

Cette surcharge de constructeur utilise le mode Compressde compression.This constructor overload uses the compression mode Compress. Pour définir le mode de compression sur une autre valeur, DeflateStream(Stream, CompressionMode) utilisez DeflateStream(Stream, CompressionMode, Boolean) la surcharge ou.To set the compression mode to another value, use the DeflateStream(Stream, CompressionMode) or DeflateStream(Stream, CompressionMode, Boolean) overload.

DeflateStream(Stream, CompressionMode)

Initialise une nouvelle instance de la classe DeflateStream à l'aide du flux et du mode de compression spécifiés.Initializes a new instance of the DeflateStream class by using the specified stream and compression mode.

public:
 DeflateStream(System::IO::Stream ^ stream, System::IO::Compression::CompressionMode mode);
public DeflateStream (System.IO.Stream stream, System.IO.Compression.CompressionMode mode);
new System.IO.Compression.DeflateStream : System.IO.Stream * System.IO.Compression.CompressionMode -> System.IO.Compression.DeflateStream

Paramètres

stream
Stream

Flux à compresser ou à décompresser.The stream to compress or decompress.

mode
CompressionMode

L'une des valeurs d'énumération qui indique s'il faut compresser ou décompresser le flux.One of the enumeration values that indicates whether to compress or decompress the stream.

Exceptions

stream a la valeur null.stream is null.

mode n’est pas une valeur de CompressionMode valide.mode is not a valid CompressionMode value.

ou-or- CompressionMode a la valeur Compress et CanWrite a la valeur false.CompressionMode is Compress and CanWrite is false.

- ou --or- CompressionMode a la valeur Decompress et CanRead a la valeur false.CompressionMode is Decompress and CanRead is false.

Exemples

L’exemple suivant montre comment utiliser la DeflateStream classe pour compresser et décompresser un fichier.The following example shows how to use the DeflateStream class to compress and decompress a file.

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

public class Program
{
    static string directoryPath = @"c:\temp";
    public static void Main()
    {
        
        DirectoryInfo directorySelected = new DirectoryInfo(directoryPath);
           Compress(directorySelected);
        

        foreach (FileInfo fileToDecompress in directorySelected.GetFiles("*.cmp"))
        {
            Decompress(fileToDecompress);
        }
    }

    public static void Compress(DirectoryInfo directorySelected)
    {
        
     
        foreach (FileInfo file in directorySelected.GetFiles("*.xml"))
        using (FileStream originalFileStream = file.OpenRead())
        {
            if ((File.GetAttributes(file.FullName) & FileAttributes.Hidden)
                != FileAttributes.Hidden & file.Extension != ".cmp")
            {
                using (FileStream compressedFileStream = File.Create(file.FullName + ".cmp"))
                {
                    using (DeflateStream compressionStream = new DeflateStream(compressedFileStream, CompressionMode.Compress))
                    {
                        originalFileStream.CopyTo(compressionStream);
                    }
                }

                FileInfo info = new FileInfo(directoryPath + "\\" + file.Name + ".cmp");
                Console.WriteLine("Compressed {0} from {1} to {2} bytes.", file.Name, file.Length, info.Length);
            }
        }
    }

    public static void Decompress(FileInfo fileToDecompress)
    {
        using (FileStream originalFileStream = fileToDecompress.OpenRead())
        {
            string currentFileName = fileToDecompress.FullName;
            string newFileName = currentFileName.Remove(currentFileName.Length - fileToDecompress.Extension.Length);

            using (FileStream decompressedFileStream = File.Create(newFileName))
            {
                using (DeflateStream decompressionStream = new DeflateStream(originalFileStream, CompressionMode.Decompress))
                {
                    decompressionStream.CopyTo(decompressedFileStream);
                    Console.WriteLine("Decompressed: {0}", fileToDecompress.Name);
                }
            }
        }
    }
}
Imports System.IO
Imports System.IO.Compression

Public Class Program
    Shared directoryPath As String = "c:\temp"
    Public Shared Sub Main()

        Dim directorySelected As New DirectoryInfo(directoryPath)
        Compress(directorySelected)


        For Each fileToDecompress As FileInfo In directorySelected.GetFiles("*.cmp")
            Decompress(fileToDecompress)
        Next
    End Sub

    Public Shared Sub Compress(directorySelected As DirectoryInfo)

        For Each file__1 As FileInfo In directorySelected.GetFiles("*.xml")
            Using originalFileStream As FileStream = file__1.OpenRead()
                If (File.GetAttributes(file__1.FullName) And FileAttributes.Hidden) <> FileAttributes.Hidden And file__1.Extension <> ".cmp" Then
                    Using compressedFileStream As FileStream = File.Create(file__1.FullName & ".cmp")
                        Using compressionStream As Compression.DeflateStream = New DeflateStream(compressedFileStream, CompressionMode.Compress)
                            originalFileStream.CopyTo(compressionStream)
                        End Using
                    End Using

                    Dim info As New FileInfo(directoryPath & "\" & file__1.Name & ".cmp")
                    Console.WriteLine("Compressed {0} from {1} to {2} bytes.", file__1.Name, file__1.Length, info.Length)
                End If
            End Using
        Next
    End Sub

    Public Shared Sub Decompress(fileToDecompress As FileInfo)
        Using originalFileStream As FileStream = fileToDecompress.OpenRead()
            Dim currentFileName As String = fileToDecompress.FullName
            Dim newFileName As String = currentFileName.Remove(currentFileName.Length - fileToDecompress.Extension.Length)

            Using decompressedFileStream As FileStream = File.Create(newFileName)
                Using decompressionStream As New DeflateStream(originalFileStream, CompressionMode.Decompress)
                    decompressionStream.CopyTo(decompressedFileStream)
                    Console.WriteLine("Decompressed: {0}", fileToDecompress.Name)
                End Using
            End Using
        End Using
    End Sub
End Class

Remarques

Par défaut, DeflateStream possède le flux sous-jacent, de sorte que la fermeture du flux ferme également le flux sous-jacent.By default, DeflateStream owns the underlying stream, so closing the stream also closes the underlying stream. Notez que l’état du flux sous-jacent peut affecter la convivialité du flux.Note that the state of the underlying stream can affect the usability of the stream. En outre, aucune vérification explicite n’est effectuée, donc aucune exception supplémentaire n’est levée lors de la création de la nouvelle instance.Also, no explicit checks are performed, so no additional exceptions are thrown when the new instance is created.

Si une instance de la DeflateStream classe est créée avec le mode paramètre égal à Compress, les informations d’en-tête sont insérées immédiatement.If an instance of the DeflateStream class is created with the mode parameter equal to Compress, header information is inserted immediately. Si aucune action supplémentaire ne se produit, le flux apparaît sous la forme d’un fichier compressé, vide et valide.If no further action occurs, the stream appears as a valid, empty, compressed file.

L’utilisation DeflateStream de la classe pour compresser un fichier d’une taille supérieure à 4 Go lève une exception.Using the DeflateStream class to compress a file larger than 4 GB raises an exception.

Par défaut, le niveau de Optimal compression a la valeur lorsque le mode de compression est. CompressBy default, the compression level is set to Optimal when the compression mode is Compress.

DeflateStream(Stream, CompressionLevel, Boolean)

Initialise une nouvelle instance de la classe DeflateStream à l'aide du flux et du niveau de compression spécifiés, et permet éventuellement de laisser le flux ouvert.Initializes a new instance of the DeflateStream class by using the specified stream and compression level, and optionally leaves the stream open.

public:
 DeflateStream(System::IO::Stream ^ stream, System::IO::Compression::CompressionLevel compressionLevel, bool leaveOpen);
public DeflateStream (System.IO.Stream stream, System.IO.Compression.CompressionLevel compressionLevel, bool leaveOpen);
new System.IO.Compression.DeflateStream : System.IO.Stream * System.IO.Compression.CompressionLevel * bool -> System.IO.Compression.DeflateStream

Paramètres

stream
Stream

Flux à compresser.The stream to compress.

compressionLevel
CompressionLevel

L'une des valeurs d'énumération qui indique s'il faut mettre l'accent sur la rapidité et l'efficacité de compression en compressant le flux de données.One of the enumeration values that indicates whether to emphasize speed or compression efficiency when compressing the stream.

leaveOpen
Boolean

true pour maintenir l'objet de flux ouvert après avoir supprimé l'objet DeflateStream ; sinon, false.true to leave the stream object open after disposing the DeflateStream object; otherwise, false.

Exceptions

stream a la valeur null.stream is null.

Le flux ne prend pas en charge les opérations d’écriture telles que la compression.The stream does not support write operations such as compression. (La propriété CanWrite sur l’objet de flux est false.)(The CanWrite property on the stream object is false.)

Exemples

L’exemple suivant montre comment définir le niveau de compression lors de la DeflateStream création d’un objet et comment le garder ouvert.The following example shows how to set the compression level when creating a DeflateStream object and how to leave the stream open.

using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string directoryPath = @"c:\users\public\reports";
            DirectoryInfo directorySelected = new DirectoryInfo(directoryPath);

            foreach (FileInfo fileToCompress in directorySelected.EnumerateFiles())
            {
                Compress(fileToCompress);
            }
        }

        public static void Compress(FileInfo fileToCompress)
        {
            using (FileStream originalFileStream = fileToCompress.OpenRead())
            {
                if ((File.GetAttributes(fileToCompress.FullName) & FileAttributes.Hidden) != FileAttributes.Hidden & fileToCompress.Extension != ".cmp")
                {
                    using (FileStream compressedFileStream = File.Create(fileToCompress.FullName + ".cmp"))
                    {
                        using (DeflateStream compressionStream = new DeflateStream(compressedFileStream, CompressionLevel.Fastest, true))
                        {
                            originalFileStream.CopyTo(compressionStream);
                        }
                        Console.WriteLine(string.Format("file compressed to {0} bytes", compressedFileStream.Length));
                    }
                }
            }
        }
    }
}
Imports System.IO
Imports System.IO.Compression

Module Module1

    Sub Main()
        Dim directoryPath As String = "c:\users\public\reports"
        Dim directorySelected As DirectoryInfo = New DirectoryInfo(directoryPath)

        For Each fileToCompress As FileInfo In directorySelected.EnumerateFiles()
            Compress(fileToCompress)
        Next
    End Sub

    Public Sub Compress(fileToCompress As FileInfo)
        Using originalFileStream As FileStream = fileToCompress.OpenRead()
            If ((File.GetAttributes(fileToCompress.FullName) & FileAttributes.Hidden) <> FileAttributes.Hidden And fileToCompress.Extension <> ".cmp") Then
                Using compressedFileStream As FileStream = File.Create(fileToCompress.FullName + ".cmp")
                    Using compressionStream As DeflateStream = New DeflateStream(compressedFileStream, CompressionLevel.Fastest, True)
                        originalFileStream.CopyTo(compressionStream)
                    End Using
                    Console.WriteLine(String.Format("file compressed to {0} bytes", compressedFileStream.Length))
                End Using
            End If
        End Using
    End Sub

End Module

Remarques

Vous utilisez ce constructeur lorsque vous souhaitez spécifier si l’efficacité de la compression ou la vitesse est plus importante pour une DeflateStream instance de la classe, et s’il faut conserver l’objet de flux DeflateStream ouvert après avoir supprimé l’objet.You use this constructor when you want to specify whether compression efficiency or speed is more important for an instance of the DeflateStream class, and whether to leave the stream object open after disposing the DeflateStream object.

Cette surcharge de constructeur utilise le mode Compressde compression.This constructor overload uses the compression mode Compress. Pour définir le mode de compression sur une autre valeur, DeflateStream(Stream, CompressionMode) utilisez DeflateStream(Stream, CompressionMode, Boolean) la surcharge ou.To set the compression mode to another value, use the DeflateStream(Stream, CompressionMode) or DeflateStream(Stream, CompressionMode, Boolean) overload.

DeflateStream(Stream, CompressionMode, Boolean)

Initialise une nouvelle instance de la classe DeflateStream à l'aide du flux et du mode de compression spécifiés, et permet éventuellement de laisser le flux ouvert.Initializes a new instance of the DeflateStream class by using the specified stream and compression mode, and optionally leaves the stream open.

public:
 DeflateStream(System::IO::Stream ^ stream, System::IO::Compression::CompressionMode mode, bool leaveOpen);
public DeflateStream (System.IO.Stream stream, System.IO.Compression.CompressionMode mode, bool leaveOpen);
new System.IO.Compression.DeflateStream : System.IO.Stream * System.IO.Compression.CompressionMode * bool -> System.IO.Compression.DeflateStream

Paramètres

stream
Stream

Flux à compresser ou à décompresser.The stream to compress or decompress.

mode
CompressionMode

L'une des valeurs d'énumération qui indique s'il faut compresser ou décompresser le flux.One of the enumeration values that indicates whether to compress or decompress the stream.

leaveOpen
Boolean

true pour maintenir le flux ouvert après avoir supprimé l'objet DeflateStream ; sinon, false.true to leave the stream open after disposing the DeflateStream object; otherwise, false.

Exceptions

stream est null.stream is null.

mode n’est pas une valeur de CompressionMode valide.mode is not a valid CompressionMode value.

- ou --or- CompressionMode a la valeur Compress et CanWrite a la valeur false.CompressionMode is Compress and CanWrite is false.

ou-or- CompressionMode a la valeur Decompress et CanRead a la valeur false.CompressionMode is Decompress and CanRead is false.

Exemples

L’exemple de code suivant montre comment utiliser la DeflateStream classe pour compresser et décompresser un fichier.The following code example shows how to use the DeflateStream class to compress and decompress a file.

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

public class Program
{
    static string directoryPath = @"c:\temp";
    public static void Main()
    {
        
        DirectoryInfo directorySelected = new DirectoryInfo(directoryPath);
           Compress(directorySelected);
        

        foreach (FileInfo fileToDecompress in directorySelected.GetFiles("*.cmp"))
        {
            Decompress(fileToDecompress);
        }
    }

    public static void Compress(DirectoryInfo directorySelected)
    {
        
     
        foreach (FileInfo file in directorySelected.GetFiles("*.xml"))
        using (FileStream originalFileStream = file.OpenRead())
        {
            if ((File.GetAttributes(file.FullName) & FileAttributes.Hidden)
                != FileAttributes.Hidden & file.Extension != ".cmp")
            {
                using (FileStream compressedFileStream = File.Create(file.FullName + ".cmp"))
                {
                    using (DeflateStream compressionStream = new DeflateStream(compressedFileStream, CompressionMode.Compress))
                    {
                        originalFileStream.CopyTo(compressionStream);
                    }
                }

                FileInfo info = new FileInfo(directoryPath + "\\" + file.Name + ".cmp");
                Console.WriteLine("Compressed {0} from {1} to {2} bytes.", file.Name, file.Length, info.Length);
            }
        }
    }

    public static void Decompress(FileInfo fileToDecompress)
    {
        using (FileStream originalFileStream = fileToDecompress.OpenRead())
        {
            string currentFileName = fileToDecompress.FullName;
            string newFileName = currentFileName.Remove(currentFileName.Length - fileToDecompress.Extension.Length);

            using (FileStream decompressedFileStream = File.Create(newFileName))
            {
                using (DeflateStream decompressionStream = new DeflateStream(originalFileStream, CompressionMode.Decompress))
                {
                    decompressionStream.CopyTo(decompressedFileStream);
                    Console.WriteLine("Decompressed: {0}", fileToDecompress.Name);
                }
            }
        }
    }
}
Imports System.IO
Imports System.IO.Compression

Public Class Program
    Shared directoryPath As String = "c:\temp"
    Public Shared Sub Main()

        Dim directorySelected As New DirectoryInfo(directoryPath)
        Compress(directorySelected)


        For Each fileToDecompress As FileInfo In directorySelected.GetFiles("*.cmp")
            Decompress(fileToDecompress)
        Next
    End Sub

    Public Shared Sub Compress(directorySelected As DirectoryInfo)

        For Each file__1 As FileInfo In directorySelected.GetFiles("*.xml")
            Using originalFileStream As FileStream = file__1.OpenRead()
                If (File.GetAttributes(file__1.FullName) And FileAttributes.Hidden) <> FileAttributes.Hidden And file__1.Extension <> ".cmp" Then
                    Using compressedFileStream As FileStream = File.Create(file__1.FullName & ".cmp")
                        Using compressionStream As Compression.DeflateStream = New DeflateStream(compressedFileStream, CompressionMode.Compress)
                            originalFileStream.CopyTo(compressionStream)
                        End Using
                    End Using

                    Dim info As New FileInfo(directoryPath & "\" & file__1.Name & ".cmp")
                    Console.WriteLine("Compressed {0} from {1} to {2} bytes.", file__1.Name, file__1.Length, info.Length)
                End If
            End Using
        Next
    End Sub

    Public Shared Sub Decompress(fileToDecompress As FileInfo)
        Using originalFileStream As FileStream = fileToDecompress.OpenRead()
            Dim currentFileName As String = fileToDecompress.FullName
            Dim newFileName As String = currentFileName.Remove(currentFileName.Length - fileToDecompress.Extension.Length)

            Using decompressedFileStream As FileStream = File.Create(newFileName)
                Using decompressionStream As New DeflateStream(originalFileStream, CompressionMode.Decompress)
                    decompressionStream.CopyTo(decompressedFileStream)
                    Console.WriteLine("Decompressed: {0}", fileToDecompress.Name)
                End Using
            End Using
        End Using
    End Sub
End Class

Remarques

Par défaut, DeflateStream possède le flux sous-jacent, de sorte que la fermeture du flux ferme également le flux sous-jacent.By default, DeflateStream owns the underlying stream, so closing the stream also closes the underlying stream. Notez que l’état du flux sous-jacent peut affecter la convivialité du flux.Note that the state of the underlying stream can affect the usability of the stream. En outre, aucune vérification explicite n’est effectuée, donc aucune exception supplémentaire n’est levée lors de la création de la nouvelle instance.Also, no explicit checks are performed, so no additional exceptions are thrown when the new instance is created.

Si une instance de la DeflateStream classe est créée avec le mode paramètre égal à Compress, les informations d’en-tête sont insérées immédiatement.If an instance of the DeflateStream class is created with the mode parameter equal to Compress, header information is inserted immediately. Si aucune action supplémentaire ne se produit, le flux apparaît sous la forme d’un fichier compressé, vide et valide.If no further action occurs, the stream appears as a valid, empty, compressed file.

L’utilisation DeflateStream de la classe pour compresser un fichier d’une taille supérieure à 4 Go lève une exception.Using the DeflateStream class to compress a file larger than 4 GB raises an exception.

Par défaut, le niveau de Optimal compression a la valeur lorsque le mode de compression est. CompressBy default, the compression level is set to Optimal when the compression mode is Compress.

S’applique à