【问题标题】:Read content of a txt file inside a folder which is inside a Zip file读取 Zip 文件内的文件夹内的 txt 文件内容
【发布时间】:2018-05-21 21:41:34
【问题描述】:

我正在尝试读取可以或不能放在文件夹中的文本文件的内容,我正在使用 DotNetZip 库来实现这一点

结构是这样的

条件一:

ZipFile/Folder(any name)/List of TXT Files.

条件二:

ZipFile/List of TXT Files.

所有文本文件都有一行数字数据,看起来像这样。

ABC.txt

1000232
1212154
4454457

我还需要在 DataTable 的单个列中列出所有 TXT 文件中的这些数字

这是我到目前为止所尝试的。

string pathtoZip = pathtotheZipfile;
        using (var zip = ZipFile.Read(pathtoZip))
        {
            int totalEntries = zip.Entries.Count;

            ZipEntry e  = null; 

            foreach (ZipEntry f in zip.Entries)
            {
                if (f.FileName.Contains("/"))
                {
                    e = f;
                    break;

                }
            }

            if (e.IsDirectory && e != null)
            {


            }
            else
            {
                foreach (ZipEntry g in zip.Entries)
                {



                }
            }


        }

用于将文本文件转换为数据表

  public DataTable TexttoDataTable(string NewFilePath)

    {
        DataTable dt = new DataTable();


        var lines = File.ReadAllLines(NewFilePath);


        dt.Columns.Add();


        for (int i = 1; i < lines.Count(); i++)
        {
            DataRow dr = dt.NewRow();
            string[] values = lines[i].Split(new char[] { ' ' });
            if (values.Length<=1)
            {
                values = lines[i].Split(new char[] { ',' });

            }

            for (int j = 0; j < values.Count(); j++)
                dr[j] = values[j];

            dt.Rows.Add(dr);
        }
        return dt;
    }

Zip 只能有一个文件夹,但文件夹名称可以不同。

【问题讨论】:

  • 你的问题是......有人填空了吗?还是您有特定的编译器或运行时错误要解决?

标签: c# dotnetzip


【解决方案1】:
static void Main(string[] args)
{
    var dt = new DataTable("Numbers");
    dt.Columns.Add(new DataColumn { DataType = typeof(int), ColumnName = "Num" });
    using (var zip = ZipFile.OpenRead(@"PATH_TO_ZIP"))
    {
        var entry = zip.GetEntry("PATH_TO_FILE_IN_ZIP");
        {
            using (var stream = entry.Open())
            {
                using (var sr = new StreamReader(stream))
                {
                    while(!sr.EndOfStream)
                    {
                        string line = sr.ReadLine();
                        var row = dt.NewRow();
                        row["Num"] = line;
                        dt.Rows.Add(row);
                    }
                }
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 2012-04-30
    • 1970-01-01
    • 1970-01-01
    • 2013-03-18
    • 2011-03-11
    • 1970-01-01
    • 2012-02-26
    • 2016-03-17
    相关资源
    最近更新 更多