【问题标题】:How to get the files from the hierarchy of Zip Folder如何从 Zip 文件夹的层次结构中获取文件
【发布时间】:2017-03-28 10:16:30
【问题描述】:

我有一个这样的目录结构: A.zip - 一种 - 一种 - 1.数据 2.数据

我想读取目录层次结构中的文件 1.dat 和 2.dat。我可以通过 C# 读取文件内容,如果文件直接位于 zip 文件夹中,但由于内部目录结构变得无法访问。

任何帮助将不胜感激。 提前致谢。

【问题讨论】:

  • 你需要让你的问题更清楚一些并添加更多信息,例如到目前为止您尝试过的一些代码

标签: c# .net zip


【解决方案1】:

不知道如何在没有示例的情况下读取 zip 文件内容,但是使用 System.IO.CompressionSystem.IO.Compression.FileSystem 程序集读取 zip 文件内容非常简单。请参阅以下示例,了解如何读取所有文件,而不考虑 zip 文件中的子目录:

using System;
using System.IO.Compression;

namespace ZipReader
{
    class Program
    {
        const string zipPath = @"D:\test\test.zip";

        static void Main(string[] args)
        {
            using (var archive = ZipFile.OpenRead(zipPath))
            {
                foreach (var entry in archive.Entries)
                {
                    Console.WriteLine(entry.FullName);
                }
            }

            Console.ReadKey();
        }
    }
}

产生以下输出:

folder1/test1.txt
folder2/test2.txt

要获取内容,您只需在每个返回 Stream 的文件上调用 entry.Open(),您可以根据需要进行处理。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-08
    • 1970-01-01
    • 2018-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多