ZipFileExtensions クラス

定義

ZipArchiveクラスとZipArchiveEntry クラスの拡張メソッドを提供します。

public ref class ZipFileExtensions abstract sealed
public static class ZipFileExtensions
type ZipFileExtensions = class
Public Module ZipFileExtensions
継承
ZipFileExtensions

次の例は、既存のファイルから zip アーカイブに新しいエントリを作成し、アーカイブの内容をディレクトリに抽出する方法を示しています。

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

次の例は、zip アーカイブの内容を反復処理し、.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

注釈

ZipFileExtensions クラスには、ZipArchiveを拡張する 4 つのメソッドが含まれています。

ZipFileExtensions クラスには、ZipArchiveEntryを拡張する 2 つのメソッドが含まれています。

メソッド

名前 説明
CreateEntryFromFile(ZipArchive, String, String, CompressionLevel, ReadOnlySpan<Char>, ZipEncryptionMethod)

ZipArchiveクラスとZipArchiveEntry クラスの拡張メソッドを提供します。

CreateEntryFromFile(ZipArchive, String, String, CompressionLevel)

指定した圧縮レベルを使用してファイルを圧縮し、zip アーカイブに追加して、ファイルをアーカイブします。

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

ZipArchiveクラスとZipArchiveEntry クラスの拡張メソッドを提供します。

CreateEntryFromFile(ZipArchive, String, String)

ファイルを圧縮して zip アーカイブに追加することで、ファイルをアーカイブします。

CreateEntryFromFileAsync(ZipArchive, String, String, CancellationToken)

指定したエントリ名の下のアーカイブに、ファイル システムからファイルを非同期に追加します。

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

指定したエントリ名の下のアーカイブに、ファイル システムからファイルを非同期に追加します。

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

ZipArchiveクラスとZipArchiveEntry クラスの拡張メソッドを提供します。

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

ZipArchiveクラスとZipArchiveEntry クラスの拡張メソッドを提供します。

ExtractToDirectory(ZipArchive, String, Boolean)

アーカイブ内のすべてのファイルをファイル システム上のディレクトリに抽出します。

ExtractToDirectory(ZipArchive, String, ZipExtractionOptions)

ZipArchiveクラスとZipArchiveEntry クラスの拡張メソッドを提供します。

ExtractToDirectory(ZipArchive, String)

zip アーカイブ内のすべてのファイルをファイル システム上のディレクトリに抽出します。

ExtractToDirectoryAsync(ZipArchive, String, Boolean, CancellationToken)

アーカイブ内のすべてのファイルをファイル システム上のディレクトリに抽出します。 指定したディレクトリが既に存在している可能性があります。

ExtractToDirectoryAsync(ZipArchive, String, CancellationToken)

アーカイブ内のすべてのファイルをファイル システム上のディレクトリに非同期的に抽出します。 指定したディレクトリは既に存在できます。

ExtractToDirectoryAsync(ZipArchive, String, ZipExtractionOptions, CancellationToken)

ZipArchiveクラスとZipArchiveEntry クラスの拡張メソッドを提供します。

ExtractToFile(ZipArchiveEntry, String, Boolean)

zip アーカイブ内のエントリをファイルに抽出し、必要に応じて同じ名前の既存のファイルを上書きします。

ExtractToFile(ZipArchiveEntry, String, ZipExtractionOptions)

ZipArchiveクラスとZipArchiveEntry クラスの拡張メソッドを提供します。

ExtractToFile(ZipArchiveEntry, String)

zip アーカイブ内のエントリをファイルに抽出します。

ExtractToFileAsync(ZipArchiveEntry, String, Boolean, CancellationToken)

エントリの内容と指定した名前を使用して、ファイル システム上にファイルを非同期に作成します。

ExtractToFileAsync(ZipArchiveEntry, String, CancellationToken)

エントリの内容と指定した名前を使用して、ファイル システム上にファイルを非同期に作成します。

ExtractToFileAsync(ZipArchiveEntry, String, ZipExtractionOptions, CancellationToken)

ZipArchiveクラスとZipArchiveEntry クラスの拡張メソッドを提供します。

適用対象