本文整理汇总了C#中System.IO.Compression.ZipFileExtensions类的典型用法代码示例。如果您正苦于以下问题:C# ZipFileExtensions类的具体用法?C# ZipFileExtensions怎么用?C# ZipFileExtensions使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ZipFileExtensions类属于System.IO.Compression命名空间,在下文中一共展示了ZipFileExtensions类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
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);
}
}
}
}
示例2: Main
//引入命名空间
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);
}
}
}
}
}