Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从ZIP C#Ionic中的特定路径提取ZIP文件_C# - Fatal编程技术网

如何从ZIP C#Ionic中的特定路径提取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.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 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,请使用其他文件夹。