-
Notifications
You must be signed in to change notification settings - Fork 1
/
Compressao.cs
52 lines (45 loc) · 1.99 KB
/
Compressao.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Linq;
using CsvHelper;
using CsvHelper.TypeConversion;
namespace ETLDespesasDeputados
{
class Compressao
{
public static IEnumerable<Origem> Conteudo(FileInfo fileToDecompress)
{
using (FileStream originalFileStream = fileToDecompress.OpenRead())
{
string currentFileName = fileToDecompress.FullName;
string newFileName = currentFileName.Remove(currentFileName.Length - fileToDecompress.Extension.Length);
using (FileStream decompressedFileStream = File.Create(newFileName))
using (GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress))
{
decompressionStream.CopyTo(decompressedFileStream);
}
var badData = new List<string>();
using (var decompressedFileStream = File.OpenText(newFileName))
using (var csvReader = new CsvReader(decompressedFileStream))
{
csvReader.Configuration.CultureInfo = System.Globalization.CultureInfo.InvariantCulture;
csvReader.Configuration.Delimiter = ",";
var options = new TypeConverterOptions
{
NumberStyle = NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign
};
csvReader.Configuration.TypeConverterOptionsCache.AddOptions(typeof(decimal), options);
csvReader.Configuration.BadDataFound = (ReadingContext readingContext) =>
{
badData.Add(readingContext.RawRecord);
};
Console.WriteLine("Obtendo registros do CSV");
return csvReader.GetRecords<Origem>().ToArray();
}
}
}
}
}