ZipArchiveEntry クラス

定義

zip アーカイブ内の圧縮ファイルを表します。

public ref class ZipArchiveEntry
public class ZipArchiveEntry
type ZipArchiveEntry = class
Public Class ZipArchiveEntry
継承
ZipArchiveEntry

最初の例では、zip アーカイブに新しいエントリを作成し、それに書き込む方法を示します。

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

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            using (FileStream zipToOpen = new FileStream(@"c:\users\exampleuser\release.zip", FileMode.Open))
            {
                using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update))
                {
                    ZipArchiveEntry readmeEntry = archive.CreateEntry("Readme.txt");
                    using (StreamWriter writer = new StreamWriter(readmeEntry.Open()))
                    {
                            writer.WriteLine("Information about this package.");
                            writer.WriteLine("========================");
                    }
                }
            }
        }
    }
}
Imports System.IO
Imports System.IO.Compression

Module Module1

    Sub Main()
        Using zipToOpen As FileStream = New FileStream("c:\users\exampleuser\release.zip", FileMode.Open)
            Using archive As ZipArchive = New ZipArchive(zipToOpen, ZipArchiveMode.Update)
                Dim readmeEntry As ZipArchiveEntry = archive.CreateEntry("Readme.txt")
                Using writer As StreamWriter = New StreamWriter(readmeEntry.Open())
                    writer.WriteLine("Information about this package.")
                    writer.WriteLine("========================")
                End Using
            End Using
        End Using
    End Sub

End Module

2 番目の例は、 ExtractToFile(ZipArchiveEntry, String) 拡張メソッドの使用方法を示しています。 コードを実行するには、プロジェクト内の System.IO.Compression.FileSystem アセンブリを参照する必要があります。

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

注釈

zip アーカイブには、圧縮された各ファイルのエントリが含まれています。 ZipArchiveEntry クラスを使用すると、エントリのプロパティを調べ、エントリを開いたり削除したりできます。 エントリを開くと、その圧縮ファイルのストリームに書き込むことで、圧縮ファイルを変更できます。

zip アーカイブとそのファイル エントリを操作するメソッドは、 ZipFileZipArchiveZipArchiveEntryの 3 つのクラスに分散されます。

宛先。。。 使用。。。
ディレクトリから zip アーカイブを作成する ZipFile.CreateFromDirectory
zip アーカイブの内容をディレクトリに抽出する ZipFile.ExtractToDirectory
既存の zip アーカイブに新しいファイルを追加する ZipArchive.CreateEntry
zip アーカイブ内のファイルを取得する ZipArchive.GetEntry
zip アーカイブ内のすべてのファイルを取得する ZipArchive.Entries
zip アーカイブに含まれる個々のファイルへのストリームを開くには ZipArchiveEntry.Open
zip アーカイブからファイルを削除する ZipArchiveEntry.Delete

プロジェクトで System.IO.Compression.FileSystem アセンブリを参照する場合は、 ZipArchiveEntry クラスの 2 つの拡張メソッドにアクセスできます。 これらのメソッドは ExtractToFile(ZipArchiveEntry, String) され、 ExtractToFile(ZipArchiveEntry, String, Boolean)され、エントリの内容をファイルに展開できます。 System.IO.Compression.FileSystem アセンブリは、Windows 8では使用できません。 Windows 8.x ストア アプリでは、DeflateStream または GZipStream を使用してアーカイブの内容を展開できます。または、Windows ランタイムの種類 CompressorDecompressor を使用してファイルを圧縮および展開できます。

プロパティ

名前 説明
Archive

エントリが属する zip アーカイブを取得します。

CompressedLength

zip アーカイブ内のエントリの圧縮サイズ (バイト単位) を取得します。

Crc32

32 ビットの循環冗長チェック。

ExternalAttributes

OS およびアプリケーション固有のファイル属性。

FullName

zip アーカイブ内のエントリの相対パスを取得します。

LastWriteTime

zip アーカイブ内のエントリが最後に変更された時刻を取得または設定します。

Length

zip アーカイブ内のエントリの非圧縮サイズ (バイト単位) を取得します。

Name

zip アーカイブ内のエントリのファイル名を取得します。

メソッド

名前 説明
Delete()

zip アーカイブからエントリを削除します。

Equals(Object)

指定したオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
MemberwiseClone()

現在の Objectの簡易コピーを作成します。

(継承元 Object)
Open()

zip アーカイブからエントリを開きます。

ToString()

zip アーカイブ内のエントリの相対パスを取得します。

拡張メソッド

名前 説明
ExtractToFile(ZipArchiveEntry, String, Boolean)

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

ExtractToFile(ZipArchiveEntry, String)

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

適用対象