ZipFileExtensions Klass
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Tillhandahåller tilläggsmetoder för klasserna ZipArchive och ZipArchiveEntry .
public ref class ZipFileExtensions abstract sealed
public static class ZipFileExtensions
type ZipFileExtensions = class
Public Module ZipFileExtensions
- Arv
-
ZipFileExtensions
Exempel
I följande exempel visas hur du skapar en ny post i ett zip-arkiv från en befintlig fil och extraherar innehållet i arkivet till en katalog.
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
I följande exempel visas hur du itererar genom innehållet i ett zip-arkiv och extraherar filer som har ett .txt-tillägg.
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
Kommentarer
Klassen ZipFileExtensions innehåller endast statiska metoder som utökar klasserna ZipArchive och ZipArchiveEntry . Du skapar inte en instans av ZipFileExtensions klassen. I stället använder du dessa metoder från instanser av ZipArchive eller ZipArchiveEntry.
Om du vill använda tilläggsmetoderna måste du referera till System.IO.Compression.FileSystem sammansättningen i projektet. Sammansättningen System.IO.Compression.FileSystem är inte tillgänglig i Windows 8.x Store-appar. Därför är klasserna ZipFileExtensions och ZipFile (som båda finns i sammansättningen System.IO.Compression.FileSystem) inte tillgängliga i Windows 8.x Store-appar. I Windows 8.x Store-appar arbetar du med komprimerade filer med hjälp av metoderna i ZipArchive, ZipArchiveEntry, DeflateStream och GZipStream.
Klassen ZipFileExtensions innehåller fyra metoder som utökar ZipArchive:
- CreateEntryFromFile(ZipArchive, String, String)
- CreateEntryFromFile(ZipArchive, String, String, CompressionLevel)
- ExtractToDirectory(ZipArchive, String)
- ExtractToDirectory(ZipArchive, String, Boolean)
Klassen ZipFileExtensions innehåller två metoder som utökar ZipArchiveEntry:
Metoder
| Name | Description |
|---|---|
| CreateEntryFromFile(ZipArchive, String, String, CompressionLevel) |
Arkiverar en fil genom att komprimera den med den angivna komprimeringsnivån och lägga till den i zip-arkivet. |
| CreateEntryFromFile(ZipArchive, String, String) |
Arkiverar en fil genom att komprimera den och lägga till den i zip-arkivet. |
| ExtractToDirectory(ZipArchive, String, Boolean) |
Extraherar alla filer i arkivet till en katalog i filsystemet. |
| ExtractToDirectory(ZipArchive, String) |
Extraherar alla filer i zip-arkivet till en katalog i filsystemet. |
| ExtractToFile(ZipArchiveEntry, String, Boolean) |
Extraherar en post i zip-arkivet till en fil och skriver eventuellt över en befintlig fil som har samma namn. |
| ExtractToFile(ZipArchiveEntry, String) |
Extraherar en post i zip-arkivet till en fil. |