ZipFileExtensions 클래스
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
및 ZipArchive 클래스에 ZipArchiveEntry 대한 확장 메서드를 제공합니다.
public ref class ZipFileExtensions abstract sealed
public static class ZipFileExtensions
type ZipFileExtensions = class
Public Module ZipFileExtensions
- 상속
-
ZipFileExtensions
설명
클래스에는 ZipFileExtensions 클래스와 ZipArchive 클래스를 확장하는 ZipArchiveEntry 정적 메서드만 포함됩니다. 클래스의 ZipFileExtensions 인스턴스를 만들지 않습니다. 대신 인스턴스 또는 ZipArchive인스턴스에서 이러한 메서드를 ZipArchiveEntry 사용합니다.
확장 메서드를 사용하려면 프로젝트에서 어셈블리를 System.IO.Compression.FileSystem 참조해야 합니다.
System.IO.Compression.FileSystem 어셈블리는 Windows 8.x 스토어 앱에서 사용할 수 없습니다. 따라서 ZipFileExtensions 및 ZipFile 클래스(둘 다 System.IO.Compression.FileSystem 어셈블리에 있음)는 Windows 8.x 스토어 앱에서 사용할 수 없습니다. Windows 8.x 스토어 앱에서는 ZipArchive, ZipArchiveEntry, DeflateStream 및 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