ZipArchiveEntry 클래스

정의

zip 보관 파일 내의 압축된 파일을 나타냅니다.

public ref class ZipArchiveEntry
public class ZipArchiveEntry
type ZipArchiveEntry = class
Public Class ZipArchiveEntry
상속
ZipArchiveEntry

설명

zip 보관 파일에는 압축된 각 파일에 대한 항목이 포함됩니다. ZipArchiveEntry 클래스를 사용하면 항목의 속성을 검사하고 항목을 열거나 삭제할 수 있습니다. 항목을 열면 압축된 파일의 스트림에 기록하여 압축된 파일을 수정할 수 있습니다.

zip 보관 파일 및 해당 파일 항목을 조작하는 메서드는 , ZipArchiveZipArchiveEntry의 세 가지 클래스에 ZipFile분산됩니다.

받는 사람... 다음을 사용...
디렉터리에서 zip 보관 파일 Create 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 액세스할 수 있습니다. 이러한 메서드는 ExtractToFile(ZipArchiveEntry, String)ExtractToFile(ZipArchiveEntry, String, Boolean)이며 파일에 대한 항목의 내용을 압축 해제할 수 있습니다. 어셈블리는 System.IO.Compression.FileSystem Windows 8 사용할 수 없습니다. Windows 8.x 스토어 앱에서는 또는 을 사용하여 DeflateStream 보관 파일의 콘텐츠를 압축 해제하거나 Windows 런타임 형식 CompressorDecompressor 사용하고 파일을 압축 및 압축 해제할 수 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

두 번째 예제에서는 확장 메서드를 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 보관 항목을 파일로 추출하고 필요에 따라 동일한 이름을 가진 기존 파일을 덮어씁니다.

적용 대상