Created
September 9, 2015 04:16
-
-
Save cmpunches/8c35c2805b03f1d3f1e2 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_montecarlo | |
{ | |
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 | |
{ | |
// something funky happening so merging this into main() for now | |
public static void extractZipEntry (string filename, string extraction_target, Stream stream) | |
{ | |
using (ZipArchive zip = ZipFile.Open(filename, ZipArchiveMode.Read)) | |
{ | |
foreach (ZipArchiveEntry entry in zip.Entries) | |
{ | |
if (entry.Name.Like(extraction_target)) | |
{ | |
Console.WriteLine("Found {0} in {1}", entry.Name, Path.GetFileName(filename)); | |
using (StreamWriter sw = new StreamWriter(stream)) | |
{ | |
sw.Write(entry.Open()); | |
} | |
} | |
} | |
} | |
} | |
static void Main(string[] args) | |
{ | |
// Stream OutStream = new FileStream(String.Format("C:\\Test-MC\\{0}", "output.txt"), FileMode.Append); | |
string singlejobpath = @"C:\1_1.zip"; | |
string[] extraction_targets = { "bob_*.txt", "jerry_*.txt" }; | |
Console.Write("Dumping Stream..."); | |
// iterate through the filenames we want to pull | |
foreach (string extraction_target in extraction_targets) | |
{ | |
// create a stream to be used | |
// we want to extract to a stream and dump in realtime | |
// after this works we'll be modifying the extracted contents as it extracts | |
using (Stream WorkStream = new MemoryStream()) | |
{ | |
using (ZipArchive zip = ZipFile.Open(singlejobpath, ZipArchiveMode.Read)) | |
{ | |
foreach (ZipArchiveEntry entry in zip.Entries) | |
{ | |
if (entry.Name.Like(extraction_target)) | |
{ | |
// we've found a file, let the user know | |
Console.WriteLine("Found {0} in {1}", entry.Name, Path.GetFileName(singlejobpath)); | |
// create an object to write to the stream | |
using (StreamWriter sw = new StreamWriter(WorkStream)) | |
{ | |
// write to the stream -- should this be happening asynchronously somehow? | |
sw.Write(entry.Open()); | |
// create an object to read from the stream | |
using (StreamReader sr = new StreamReader(WorkStream)) | |
{ | |
// since we are writing by line for output, we'll buffer by line | |
string line = ""; | |
// read from the stream | |
while ((line = sr.ReadLine()) != null) | |
{ | |
// print the contents to the user by line | |
Console.WriteLine(line); | |
} | |
} | |
// Exception thrown here: | |
// System.ObjectDisposedException: Cannot access a closed stream. | |
} | |
} | |
} | |
} | |
} | |
} | |
// cli tool, this is just for testing | |
Console.ReadLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment