Last active
December 19, 2015 19:48
-
-
Save vermorel/6008628 to your computer and use it in GitHub Desktop.
Tiny stand-alone flat file sampling utility.
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.IO; | |
using System.IO.Compression; | |
using System.Text; | |
namespace Loader | |
{ | |
class FlatSampling | |
{ | |
static void Main(string[] args) | |
{ | |
if (args.Length != 2) | |
{ | |
Console.WriteLine( | |
@"Flat file sampling utility. | |
Usage | |
flatsamping.exe filename samplerate | |
Where: | |
'filename' is the target file | |
'samplerate' is an integer representing sampling rate | |
"); | |
return; | |
} | |
string source = args[0]; | |
var rate = int.Parse(args[1]); | |
if (source.EndsWith(".gz")) | |
{ | |
using (var file = new FileStream(source, FileMode.Open)) | |
using (var zip = new GZipStream(file, CompressionMode.Decompress)) | |
using (var reader = new StreamReader(zip, Encoding.UTF8)) | |
{ | |
Read(reader, rate); | |
} | |
} | |
else | |
{ | |
using (var file = new FileStream(source, FileMode.Open)) | |
using (var reader = new StreamReader(file, Encoding.UTF8)) | |
{ | |
Read(reader, rate); | |
} | |
} | |
} | |
private static void Read(StreamReader reader, int rate) | |
{ | |
int lineIndex = 0; | |
string line = null; | |
while ((line = reader.ReadLine()) != null) | |
{ | |
if(lineIndex++ % rate == 0) Console.WriteLine(line); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment