using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; //using System.IO.Compression; using System.Threading; using System.Text.RegularExpressions; using Syncfusion.Compression.Zip; using System.Runtime.InteropServices; using System.Data; namespace CLX_Archiver { internal class Program { static void Main(string[] args) { DataSet ds = new DataSet(); string outpath = ""; string protocollpath = ""; string filenameind = ""; string ln = ""; string datum = ""; int jahr = 0; if (args.Count() < 1) { return; } outpath = args[1]; protocollpath = args[1] + DateTime.Now.ToString("yyyyMMddhhmmss") + "_protokoll.log"; if (System.IO.File.Exists(args[1] + "inddone.xml")) { ds.ReadXml(args[1] + "inddone.xml"); } else { DataTable dt = new DataTable(); dt.Columns.Add("indfile"); ds.Tables.Add(dt); } StreamWriter writer = new StreamWriter(protocollpath); writer.WriteLine("DatumZeit;Dateiname;Datum;Jahr;ZIP Index;ZIP Daten;Resultat"); foreach (string filename in Directory.EnumerateFiles(args[0], "*.ind", SearchOption.AllDirectories)) { Console.WriteLine(filename); bool fertig = false; while (fertig == false) { using (StreamReader file = new StreamReader(filename)) { int counter = 0; while ((ln = file.ReadLine()) != null && fertig == false) { if (ln.ToUpper() == "GROUP_FIELD_NAME:ARCHIVDATE") { ln = file.ReadLine(); Regex rgx = new Regex(@"\d{2}.\d{2}.\d{4}"); Match mat = rgx.Match(ln); datum = mat.ToString(); jahr = Convert.ToInt32(datum.Substring((datum.Length - 4))); datum = datum.Replace(".", "_"); fertig = true; } } file.Close(); } } bool bereits_verarbeitet = false; foreach (DataRow dr in ds.Tables[0].Rows) { if (dr[0].ToString() == System.IO.Path.GetFileName(filename)) { bereits_verarbeitet = true; } } string fn = ""; fn = System.IO.Path.GetFileName(filename); if ((jahr > 2012) && (bereits_verarbeitet==false)) { fn = System.IO.Path.GetFileName(filename); string indfile = filename; string indfilezip = args[1] + datum+"_"+System.IO.Path.GetFileName(indfile) + ".zip"; string outfile = indfile.Replace(".ind", ".out"); string outfilezip = args[1] + datum + "_"+System.IO.Path.GetFileName(outfile) + ".zip"; if (System.IO.File.Exists(indfile) && System.IO.File.Exists(outfile)) { if (System.IO.File.Exists(indfilezip)){ indfilezip = newfilename(args[1], datum, System.IO.Path.GetFileName(indfile)); } if (System.IO.File.Exists(outfilezip)){ outfilezip = newfilename(args[1], datum, System.IO.Path.GetFileName(outfile)); } string res = ""; res = CompressFile(indfile, indfilezip); if (res !="") { writer.WriteLine(DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss")+";"+ fn + ";" + datum + ";" + jahr.ToString() + ";" + System.IO.Path.GetFileName(indfilezip) + ";" + System.IO.Path.GetFileName(outfilezip) + ";Error ZIP"); } res = CompressFile(outfile, outfilezip); if (res != "") { writer.WriteLine(DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss") + ";" + fn + ";" + datum + ";" + jahr.ToString() + ";" + System.IO.Path.GetFileName(indfilezip) + ";" + System.IO.Path.GetFileName(outfilezip) + ";Error ZIP"); } } writer.WriteLine(DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss") + ";" + fn + ";" + datum + ";" + jahr.ToString() + ";" + System.IO.Path.GetFileName(indfilezip) + ";"+ System.IO.Path.GetFileName(outfilezip) +";done"); DataRow d = ds.Tables[0].NewRow(); d[0] = fn; ds.Tables[0].Rows.Add(d); } else { if (bereits_verarbeitet == false) { writer.WriteLine(DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss") + ";" + fn + ";" + datum + ";" + jahr.ToString() + ";" + "" + ";" + "" + ";out of range"); } } } writer.Close(); ds.WriteXml(args[1] + "inddone.xml"); } private static string newfilename(string path, string datum, string filename) { string newfn = ""; int cnt = 0; newfn = path + datum + "_" + filename + ".zip"; while (System.IO.File.Exists(newfn)) { newfn = path + datum + "_" + cnt.ToString() + "_" + filename + ".zip"; cnt=cnt + 1; } return newfn; } private static string CompressFile(string sourceFileName, string destfilename) { try { ZipArchive zipArchive = new Syncfusion.Compression.Zip.ZipArchive(); zipArchive.DefaultCompressionLevel = Syncfusion.Compression.CompressionLevel.Best; zipArchive.AddFile(sourceFileName); zipArchive.Save(destfilename); zipArchive = null; return ""; } catch (Exception e) { return "Error: " + e.Message; } } } }