ZipFileExtensions 클래스

정의

ZipArchiveZipArchiveEntry 클래스에 대한 확장 메서드를 제공합니다.

public ref class ZipFileExtensions abstract sealed
public static class ZipFileExtensions
type ZipFileExtensions = class
Public Module ZipFileExtensions
상속
ZipFileExtensions

설명

클래스에는 ZipFileExtensionsZipArchiveEntry 클래스를 확장하는 ZipArchive 정적 메서드만 포함됩니다. 클래스의 ZipFileExtensions 인스턴스를 만들지 않습니다. 대신 또는 ZipArchiveEntry인스턴스 ZipArchive 에서 이러한 메서드를 사용합니다.

확장 메서드를 사용하려면 프로젝트에서 어셈블리를 System.IO.Compression.FileSystem 참조해야 합니다. 어셈블리는 System.IO.Compression.FileSystem Windows 8.x 스토어 앱에서 사용할 수 없습니다. 따라서 ZipFileExtensionsZipFile 클래스(둘 다 어셈블리에 System.IO.Compression.FileSystem 있음)는 Windows 8.x 스토어 앱에서 사용할 수 없습니다. Windows 8.x 스토어 앱에서는 , , ZipArchiveEntryDeflateStream및 의 ZipArchive메서드를 사용하여 압축된 파일로 작업합니다GZipStream.

클래스에는 ZipFileExtensions 를 확장하는 ZipArchive네 가지 메서드가 포함되어 있습니다.

클래스에는 ZipFileExtensions 를 확장하는 ZipArchiveEntry두 가지 메서드가 포함되어 있습니다.

예제

다음 예제에서는 기존 파일에서 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

메서드

CreateEntryFromFile(ZipArchive, String, String)

파일을 압축하고 ZIP 보관 위치에 추가하여 보관합니다.

CreateEntryFromFile(ZipArchive, String, String, CompressionLevel)

지정된 압축 수준을 사용하여 압축하고 zip 보관 저장소에 추가하여 파일을 보관합니다.

ExtractToDirectory(ZipArchive, String)

Zip 보관 파일의 모든 파일을 파일 시스템의 디렉터리에 추출합니다.

ExtractToDirectory(ZipArchive, String, Boolean)

보관 파일의 모든 파일을 파일 시스템의 디렉터리에 추출합니다.

ExtractToFile(ZipArchiveEntry, String)

ZIP 보관 파일의 항목을 파일에 추출합니다.

ExtractToFile(ZipArchiveEntry, String, Boolean)

zip 보관 항목을 파일로 추출하고 필요에 따라 동일한 이름을 가진 기존 파일을 덮어씁니다.

적용 대상