ZipFileExtensions Classe

Definizione

Fornisce metodi di estensione per le ZipArchive classi e ZipArchiveEntry .

public ref class ZipFileExtensions abstract sealed
public static class ZipFileExtensions
type ZipFileExtensions = class
Public Module ZipFileExtensions
Ereditarietà
ZipFileExtensions

Esempio

Nell'esempio seguente viene illustrato come creare una nuova voce in un archivio ZIP da un file esistente ed estrarre il contenuto dell'archivio in una directory.

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

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string zipPath = @"c:\users\exampleuser\start.zip";
            string extractPath = @"c:\users\exampleuser\extract";
            string newFile = @"c:\users\exampleuser\NewFile.txt";

            using (ZipArchive archive = ZipFile.Open(zipPath, ZipArchiveMode.Update))
            {
                archive.CreateEntryFromFile(newFile, "NewEntry.txt");
                archive.ExtractToDirectory(extractPath);
            }
        }
    }
}
Imports System.IO
Imports System.IO.Compression

Module Module1

    Sub Main()
        Dim zipPath As String = "c:\users\exampleuser\end.zip"
        Dim extractPath As String = "c:\users\exampleuser\extract"
        Dim newFile As String = "c:\users\exampleuser\NewFile.txt"

        Using archive As ZipArchive = ZipFile.Open(zipPath, ZipArchiveMode.Update)
            archive.CreateEntryFromFile(newFile, "NewEntry.txt", CompressionLevel.Fastest)
            archive.ExtractToDirectory(extractPath)
        End Using
    End Sub

End Module

Nell'esempio seguente viene illustrato come scorrere il contenuto di un archivio ZIP ed estrarre file con estensione .txt.

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

class Program
{
    static void Main(string[] args)
    {
        string zipPath = @".\result.zip";

        Console.WriteLine("Provide path where to extract the zip file:");
        string extractPath = Console.ReadLine();

        // Normalizes the path.
        extractPath = Path.GetFullPath(extractPath);

        // Ensures that the last character on the extraction path
        // is the directory separator char.
        // Without this, a malicious zip file could try to traverse outside of the expected
        // extraction path.
        if (!extractPath.EndsWith(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal))
            extractPath += Path.DirectorySeparatorChar;

        using (ZipArchive archive = ZipFile.OpenRead(zipPath))
        {
            foreach (ZipArchiveEntry entry in archive.Entries)
            {
                if (entry.FullName.EndsWith(".txt", StringComparison.OrdinalIgnoreCase))
                {
                    // Gets the full path to ensure that relative segments are removed.
                    string destinationPath = Path.GetFullPath(Path.Combine(extractPath, entry.FullName));

                    // Ordinal match is safest, case-sensitive volumes can be mounted within volumes that
                    // are case-insensitive.
                    if (destinationPath.StartsWith(extractPath, StringComparison.Ordinal))
                        entry.ExtractToFile(destinationPath);
                }
            }
        }
    }
}
Imports System.IO
Imports System.IO.Compression

Module Module1

    Sub Main()
        Dim zipPath As String = ".\result.zip"

        Console.WriteLine("Provide path where to extract the zip file:")
        Dim extractPath As String = Console.ReadLine()

        ' Normalizes the path.
        extractPath = Path.GetFullPath(extractPath)

        ' Ensures that the last character on the extraction path
        ' is the directory separator char. 
        ' Without this, a malicious zip file could try to traverse outside of the expected
        ' extraction path.
        If Not extractPath.EndsWith(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal) Then
            extractPath += Path.DirectorySeparatorChar
        End If

        Using archive As ZipArchive = ZipFile.OpenRead(zipPath)
            For Each entry As ZipArchiveEntry In archive.Entries
                If entry.FullName.EndsWith(".txt", StringComparison.OrdinalIgnoreCase) Then

                    ' Gets the full path to ensure that relative segments are removed.
                    Dim destinationPath As String = Path.GetFullPath(Path.Combine(extractPath, entry.FullName))
                    
                    ' Ordinal match is safest, case-sensitive volumes can be mounted within volumes that
                    ' are case-insensitive.
                    If destinationPath.StartsWith(extractPath, StringComparison.Ordinal) Then 
                        entry.ExtractToFile(destinationPath)
                    End If

                End If
            Next
        End Using
    End Sub

End Module

Commenti

La ZipFileExtensions classe contiene quattro metodi che estendono ZipArchive:

La ZipFileExtensions classe contiene due metodi che estendono ZipArchiveEntry:

Metodi

Nome Descrizione
CreateEntryFromFile(ZipArchive, String, String, CompressionLevel, ReadOnlySpan<Char>, ZipEncryptionMethod)

Fornisce metodi di estensione per le ZipArchive classi e ZipArchiveEntry .

CreateEntryFromFile(ZipArchive, String, String, CompressionLevel)

Archivia un file comprimendolo usando il livello di compressione specificato e aggiungendolo all'archivio ZIP.

CreateEntryFromFile(ZipArchive, String, String, ReadOnlySpan<Char>, ZipEncryptionMethod)

Fornisce metodi di estensione per le ZipArchive classi e ZipArchiveEntry .

CreateEntryFromFile(ZipArchive, String, String)

Archivia un file comprimendolo e aggiungendolo all'archivio ZIP.

CreateEntryFromFileAsync(ZipArchive, String, String, CancellationToken)

Aggiunge in modo asincrono un file dal file system all'archivio con il nome della voce specificato.

CreateEntryFromFileAsync(ZipArchive, String, String, CompressionLevel, CancellationToken)

Aggiunge in modo asincrono un file dal file system all'archivio con il nome della voce specificato.

CreateEntryFromFileAsync(ZipArchive, String, String, CompressionLevel, ReadOnlyMemory<Char>, ZipEncryptionMethod, CancellationToken)

Fornisce metodi di estensione per le ZipArchive classi e ZipArchiveEntry .

CreateEntryFromFileAsync(ZipArchive, String, String, ReadOnlyMemory<Char>, ZipEncryptionMethod, CancellationToken)

Fornisce metodi di estensione per le ZipArchive classi e ZipArchiveEntry .

ExtractToDirectory(ZipArchive, String, Boolean)

Estrae tutti i file nell'archivio in una directory nel file system.

ExtractToDirectory(ZipArchive, String, ZipExtractionOptions)

Fornisce metodi di estensione per le ZipArchive classi e ZipArchiveEntry .

ExtractToDirectory(ZipArchive, String)

Estrae tutti i file nell'archivio ZIP in una directory nel file system.

ExtractToDirectoryAsync(ZipArchive, String, Boolean, CancellationToken)

Estrae tutti i file nell'archivio in una directory nel file system. È possibile che la directory specificata esista già.

ExtractToDirectoryAsync(ZipArchive, String, CancellationToken)

Estrae in modo asincrono tutti i file nell'archivio in una directory nel file system. La directory specificata può già esistere.

ExtractToDirectoryAsync(ZipArchive, String, ZipExtractionOptions, CancellationToken)

Fornisce metodi di estensione per le ZipArchive classi e ZipArchiveEntry .

ExtractToFile(ZipArchiveEntry, String, Boolean)

Estrae una voce nell'archivio ZIP in un file e, facoltativamente, sovrascrive un file esistente con lo stesso nome.

ExtractToFile(ZipArchiveEntry, String, ZipExtractionOptions)

Fornisce metodi di estensione per le ZipArchive classi e ZipArchiveEntry .

ExtractToFile(ZipArchiveEntry, String)

Estrae una voce nell'archivio ZIP in un file.

ExtractToFileAsync(ZipArchiveEntry, String, Boolean, CancellationToken)

Crea in modo asincrono un file nel file system con il contenuto della voce e il nome specificato.

ExtractToFileAsync(ZipArchiveEntry, String, CancellationToken)

Crea in modo asincrono un file nel file system con il contenuto della voce e il nome specificato.

ExtractToFileAsync(ZipArchiveEntry, String, ZipExtractionOptions, CancellationToken)

Fornisce metodi di estensione per le ZipArchive classi e ZipArchiveEntry .

Si applica a