using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.IO.Compression; using System.Text.RegularExpressions; namespace cli_20150929 { public static class StringExtensions { /// /// Compares the string against a given pattern. /// /// The string. /// The pattern to match, where "*" means any sequence of characters, and "?" means any single character. /// true if the string matches the given pattern; otherwise false. public static bool Like(this string str, string pattern) { return new Regex( "^" + Regex.Escape(pattern).Replace(@"\*", ".*").Replace(@"\?", ".") + "$", RegexOptions.IgnoreCase | RegexOptions.Singleline ).IsMatch(str); } } class Program { public static Stream ExtractZipEntrytoStream(string inputFilePath, string entryFileNamePattern) { ZipArchive zip = ZipFile.Open(inputFilePath, ZipArchiveMode.Read); foreach (ZipArchiveEntry entry in zip.Entries) { if (entry.Name.Like(entryFileNamePattern)) { Console.WriteLine("Found {0} in {1}", entry.Name, Path.GetFileName(inputFilePath)); var OutStream = entry.Open(); return OutStream; } } return null; } public static IEnumerable FilterData(IEnumerable input) { foreach (string line in input) { yield return line.ToLower(); } } public static IEnumerable FilterDataDifferently(IEnumerable input) { foreach (string line in input) { yield return line.ToUpper(); } } public static void DumpStreamToConsole(Stream inStream) { var streamReader = new StreamReader(inStream, Encoding.UTF8, true, 8192, true); string line; while ((line = streamReader.ReadLine()) != null) { Console.WriteLine(line); } } public static void DumpEnumeratorToConsole(IEnumerable input) { int i = 0; foreach (string line in input) { i++; if (i % (1024*1024) == 0) { Console.WriteLine("{0} MB processed as of {1}.", (i / (1024*1024)), DateTime.Now.ToString()); } } } public static IEnumerable EnumeratedStream(Stream inStream) { var streamReader = new StreamReader(inStream, Encoding.UTF8, true, 8192, true); if (inStream != null) { for (string i = streamReader.ReadLine(); i != null; i = streamReader.ReadLine()) { yield return (string)i; } } } static void Main(string[] args) { // just point a single iteration for now at some test data string inputFilePath = @"C:\Users\punchc1\Desktop\COBRA_SVN\trunk\External Customer\BiPoshv3\Test-Data\6503\162_1.zip"; string[] extractionTargets = { "InstrumentTrial_*.txt", "TrancheTrial_*.txt" }; if (File.Exists(inputFilePath)) { // Console.WriteLine("Scanning {0} for {1}...", Path.GetFileName(inputFilePath), extraction_targets[0]); Console.WriteLine("Scanning {0} for {1}...", inputFilePath, extractionTargets[0]); Stream workStream = ExtractZipEntrytoStream(inputFilePath, extractionTargets[0]); if (workStream != null) { // DO NOT KEEP STREAM IN MEMORY DumpEnumeratorToConsole( FilterData( FilterDataDifferently( EnumeratedStream( workStream ) ) ) ); } else { Console.WriteLine("No match was found."); } } else { Console.WriteLine("{0} doesn't exist.", inputFilePath); } Console.WriteLine("Complete. Press a key to continue..."); Console.ReadLine(); } } }