Skip to content

Instantly share code, notes, and snippets.

@cmpunches
Created October 6, 2015 00:06
Show Gist options
  • Save cmpunches/86b39b29d203ae677d2d to your computer and use it in GitHub Desktop.
Save cmpunches/86b39b29d203ae677d2d to your computer and use it in GitHub Desktop.

Revisions

  1. Chris Punches created this gist Oct 6, 2015.
    145 changes: 145 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,145 @@
    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, 8192, true);

    string line;
    while ((line = streamReader.ReadLine()) != null)
    {
    Console.WriteLine(line);
    }
    }

    public static void DumpEnumeratorToConsole(IEnumerable<string> 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<string> 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();
    }
    }
    }