Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@cmpunches
Last active September 22, 2015 06:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmpunches/abf6a6bf2537c4c82a85 to your computer and use it in GitHub Desktop.
Save cmpunches/abf6a6bf2537c4c82a85 to your computer and use it in GitHub Desktop.
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_20150921
{
public class Stream : System.IO.Stream
{
public override bool CanWrite
{
get
{
return CanWrite;
}
set
{
CanWrite = value;
}
}
public override bool CanRead
{
get
{
return CanRead;
}
set
{
CanRead = value;
}
}
}
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)
{
using (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));
return entry.Open();
}
}
}
return null;
}
public static Stream ProcessExtractedStream(Stream InStream)
{
MemoryStream OutStream = new System.IO.MemoryStream();
using (StreamReader streamReader = new StreamReader(InStream))
{
string line;
while ((line = streamReader.ReadLine()) != null)
{
using (StreamWriter streamWriter = new StreamWriter(OutStream))
{
streamWriter.WriteLine(line);
}
}
}
return OutStream;
}
public static Stream ProcessExtractedStreamDifferently(Stream InStream)
{
Stream OutStream = new Stream();
using (StreamReader streamReader = new StreamReader(InStream))
{
string line;
while ((line = streamReader.ReadLine()) != null)
{
using (StreamWriter streamWriter = new StreamWriter(OutStream))
{
streamWriter.WriteLine(line.ToUpper());
}
}
}
return OutStream;
}
public static Stream DumpStreamToConsole(Stream stream)
{
using (StreamReader streamReader = new StreamReader(stream))
{
string line = streamReader.ReadLine();
if (!line.Equals(null))
{
using (StreamWriter streamWriter = new StreamWriter(stream))
{
streamWriter.WriteLine(line.ToUpper());
}
}
}
return stream;
}
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\1_1.zip";
string[] extraction_targets = { "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, extraction_targets[0]);
Stream WorkStream = ExtractZipEntrytoStream(inputFilePath, extraction_targets[0]);
if (WorkStream != null)
{
WorkStream = ProcessExtractedStream(WorkStream);
WorkStream = ProcessExtractedStreamDifferently(WorkStream);
DumpStreamToConsole(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