Last active
September 29, 2015 07:42
-
-
Save cmpunches/91adfb71a8f4a572dc51 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
{ | |
/// <summary> | |
/// Compares the string against a given pattern. | |
/// </summary> | |
/// <param name="str">The string.</param> | |
/// <param name="pattern">The pattern to match, where "*" means any sequence of characters, and "?" means any single character.</param> | |
/// <returns><c>true</c> if the string matches the given pattern; otherwise <c>false</c>.</returns> | |
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<string> FilterData(IEnumerable<string> input) | |
{ | |
foreach (string line in input) | |
{ | |
yield return line.ToLower(); | |
} | |
} | |
public static IEnumerable<string> FilterDataDifferently(IEnumerable<string> input) | |
{ | |
foreach (string line in input) | |
{ | |
yield return line.ToUpper(); | |
} | |
} | |
public static void DumpStreamToConsole(Stream inStream) | |
{ | |
var streamReader = new StreamReader(inStream, Encoding.UTF8, true, 4096, true); | |
string line; | |
while ((line = streamReader.ReadLine()) != null) | |
{ | |
Console.WriteLine(line); | |
} | |
} | |
public static void DumpEnumeratorToConsole(IEnumerable<string> input) | |
{ | |
foreach (string line in input) | |
{ | |
Console.WriteLine(line); | |
} | |
} | |
public static IEnumerable<string> EnumeratedStream(Stream inStream) | |
{ | |
var streamReader = new StreamReader(inStream, Encoding.UTF8, true, 4096, 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) | |
{ | |
// BEAST MODE ACTIVATE | |
DumpEnumeratorToConsole( | |
FilterData( | |
FilterDataDifferently( | |
EnumeratedStream( | |
workStream | |
) | |
) | |
) | |
); | |
} | |
else | |
{ | |
Console.WriteLine("No match was found."); | |
} | |
} | |
else | |
{ | |
Console.WriteLine("{0} doesn't exist.", inputFilePath); | |
} | |
Console.ReadLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment