ZipArchiveEntry クラス

定義

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

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

注釈

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

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

宛先... 用途
ディレクトリから 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 参照する場合は、 クラスの 2 つの拡張メソッドに ZipArchiveEntry アクセスできます。 これらのメソッドは ExtractToFile(ZipArchiveEntry, String)ExtractToFile(ZipArchiveEntry, String, Boolean)であり、エントリの内容をファイルに展開できます。 アセンブリはSystem.IO.Compression.FileSystem、Windows 8では使用できません。 Windows 8.x ストア アプリでは、 または を使用DeflateStreamしてアーカイブの内容を圧縮解除したり、Windows ランタイムの種類Compressorを使用したり、ファイルの圧縮とDecompressor圧縮解除を行ったりGZipStreamできます。

最初の例では、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

プロパティ

Archive

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

Comment

省略可能なエントリ コメントを取得または設定します。

CompressedLength

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

Crc32

32 ビットの巡回冗長検査。

ExternalAttributes

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

FullName

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

IsEncrypted

エントリが暗号化されているかどうかを示す値を取得します。

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)

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

ExtractToFile(ZipArchiveEntry, String, Boolean)

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

適用対象