Ejemplo n.º 1
0
        public FileReader(IFileStreamWrap stream)
        {
            if (stream == null) throw new ArgumentNullException(nameof(stream));

            //Asses file ext
            var ext = Path.GetExtension(stream.Name);
            if (ext == ".gz")
            {
                Compression = CompressionScheme.GZip;
            }

            //Assign stream
            Stream = stream;

            //If not testing assign actual stream
            if (stream.FileStreamInstance != null)
            {
                if (Compression == CompressionScheme.None)
                {
                    _reader = new StreamReaderWrap(stream.FileStreamInstance);
                }
                else if (Compression == CompressionScheme.GZip)
                {
                    // Toggle between the x86 and x64 bit dll
                   // var path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),Environment.Is64BitProcess ? "x64" : "x86", "7z.dll");
                    SevenZipExtractor.SetLibraryPath("C:\\Program Files (x86)\\7-Zip\\7z.dll");

                    _gzipStream = new SevenZipExtractor(stream.FileStreamInstance);
                    //_memoryStream = new MemoryStream();
                    //_gzipStream.ExtractFile(0, _memoryStream);
                    _gzipStream.ExtractArchive("D:\\test.txt");
                    _reader = new StreamReaderWrap(_memoryStream);              
                }
            }
        }
Ejemplo n.º 2
0
        private IList <string[]> ParseCSV(string path)
        {
            var parsedData = new List <string[]>();

            try
            {
                if (StreamReader == null)
                {
                    StreamReader = new StreamReaderWrap(path);
                }

                StreamReader.Initialize(path);

                using (var readFile = StreamReader)
                {
                    string line;

                    while ((line = readFile.ReadLine()) != null)
                    {
                        //string[] row = line.Split(',');

                        var row = new List <string>();

                        // split line into values but not withing quote signs
                        int  startIndex   = 0;
                        bool withinQuotes = false;
                        for (int i = 0; i < line.Length; i++)
                        {
                            char character = line[i];

                            if (character.Equals('\"'))
                            {
                                withinQuotes = !withinQuotes;
                            }

                            // split
                            if (character.Equals(',') && !withinQuotes)
                            {
                                row.Add(line.Substring(startIndex, i - startIndex));
                                startIndex = i + 1;
                            }
                        }

                        // add last entry
                        row.Add(line.Substring(startIndex, line.Length - startIndex));

                        parsedData.Add(row.ToArray());
                    }
                }
            }
            catch (IOException e)
            {
                throw new TagReaderException("Cant read file " + path, path, e);
            }

            return(parsedData);
        }
Ejemplo n.º 3
0
        public void Dispose()
        {
            _memoryStream?.Dispose();
            _gzipStream?.Dispose();

            if (_reader != null)
            {
                _reader.Close();
                _reader = null;
            }

            Stream = null;
        }