I am trying to read the content of text files which can or can not be placed inside a folder I am using DotNetZip Library to achieve this
The structure is something like this
Condition 1 :
ZipFile/Folder(any name)/List of TXT Files.
Condition 2 :
ZipFile/List of TXT Files.
All the text files have a single row of numerical data which looks something like this .
ABC.txt
1000232
1212154
4454457
I also need list of these numbers from all the TXT files in a single column of a DataTable
This is what I have tried till now.
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)
{
}
}
}
For converting Text File to A DaTaTable
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;
}
The Zip can have only one folder but the name of the folder can be different.
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);
}
}
}
}
}
}