在C#中提取ZIP文件可以使用System.IO.Compression命名空间中的ZipArchive类。以下是一个完整的示例代码:
using System;
using System.IO;
using System.IO.Compression;
class Program
{
static void Main()
{
string zipPath = "path/to/your/zipfile.zip";
string extractPath = "path/to/extract/files";
using (ZipArchive archive = ZipFile.OpenRead(zipPath))
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
string entryPath = Path.Combine(extractPath, entry.FullName);
if (entryPath.EndsWith("/"))
{
Directory.CreateDirectory(entryPath);
}
else
{
entry.ExtractToFile(entryPath, overwrite: true);
}
}
}
Console.WriteLine("ZIP file extracted successfully.");
}
}
上述代码中,你需要将zipPath
替换为你要提取的ZIP文件的路径,将extractPath
替换为你要提取文件的目标路径。代码首先使用ZipFile.OpenRead
方法打开ZIP文件,然后遍历ZIP文件中的每个条目。如果条目是一个目录,则创建相应的目录;如果是一个文件,则使用ExtractToFile
方法将文件提取到指定的目标路径中。
这是一个基本的ZIP文件提取示例,你可以根据实际需求进行修改和扩展。如果你想了解更多关于C#中提取ZIP文件的方法,可以参考以下链接:
领取专属 10元无门槛券
手把手带您无忧上云