如何从ZIP C#Ionic中的特定路径提取ZIP文件
我有一个名为abc.ZIP的ZIP文件 内部ZIP文件夹结构如下:如何从ZIP C#Ionic中的特定路径提取ZIP文件,c#,C#,我有一个名为abc.ZIP的ZIP文件 内部ZIP文件夹结构如下: --abc ---pqr ----a ----b ----c 我想在D:/folder\u name处提取此ZIP文件 但我只想提取文件夹及其名为a、b、c的内容。文件夹名称也不是固定的。我不想提取根文件夹abc及其子文件夹pqr 我使用了以下代码,但不起作用: using (ZipFile zipFile = ZipFile.Read(@"temp.zip")) { foreach (ZipEntry entry i
--abc
---pqr
----a
----b
----c
我想在D:/folder\u name处提取此ZIP文件
但我只想提取文件夹及其名为a、b、c的内容。文件夹名称也不是固定的。我不想提取根文件夹abc及其子文件夹pqr
我使用了以下代码,但不起作用:
using (ZipFile zipFile = ZipFile.Read(@"temp.zip"))
{
foreach (ZipEntry entry in zipFile.Entries)
{
entry.Extract(@"D:/folder_name");
}
}
下面的方法应该有效,但我不确定这是否是最好的选择
string rootPath = "abc/pqr/";
using (ZipFile zipFile = ZipFile.Read(@"abc.zip"))
{
foreach (ZipEntry entry in zipFile.Entries)
{
if (entry.FileName.StartsWith(rootPath) && entry.FileName.Length > rootPath.Length)
{
string path = Path.Combine(@"D:/folder_name", entry.FileName.Substring(rootPath.Length));
if (entry.IsDirectory)
{
Directory.CreateDirectory(path);
}
else
{
using (FileStream stream = new FileStream(path, FileMode.Create))
entry.Extract(stream);
}
}
}
}
另一种选择是在临时目录中提取完整文件,并将子目录移动到目标目录。以下操作应该可以,但我不确定这是否是最佳选择
string rootPath = "abc/pqr/";
using (ZipFile zipFile = ZipFile.Read(@"abc.zip"))
{
foreach (ZipEntry entry in zipFile.Entries)
{
if (entry.FileName.StartsWith(rootPath) && entry.FileName.Length > rootPath.Length)
{
string path = Path.Combine(@"D:/folder_name", entry.FileName.Substring(rootPath.Length));
if (entry.IsDirectory)
{
Directory.CreateDirectory(path);
}
else
{
using (FileStream stream = new FileStream(path, FileMode.Create))
entry.Extract(stream);
}
}
}
}
另一个选项是提取临时目录中的完整文件,并将子目录移动到目标目录。我只使用第二个选项(使用temp Dir),但现在问题是在部署服务器上,我无法访问temp。您可以在任何文件夹中创建临时文件夹。如果您无法访问temp,请使用另一个文件夹。我只使用第二个选项(使用temp Dir),但现在的问题是在部署服务器上,我无法访问temp。您可以在任何文件夹中创建临时文件夹。如果您无权访问temp,请使用其他文件夹。