Search code examples
c#gzipfile.readalllines

How can I used ReadAllLines with gzipped file


Is there a way to use the one-liner ReadAllLines on a gzipped file?

var pnDates = File.ReadAllLines("C:\myfile.gz");

Can I put GZipStream wrapper around the file some how?


Solution

  • No, File.ReadAllLines() treats the file specified as text file. A zipfile isn't that. It's trivial to do it yourself:

    public IEnumerable<string> ReadAllZippedLines(string filename)
    {
        using (var fileStream = File.OpenRead(filename))
        {
            using (var gzipStream = new GZipStream(fileStream, CompressionMode.Decompress))
            {
                using (var reader = new StreamReader(gzipStream)) 
                {
                    yield return reader.ReadLine();
                }
            }
        }
    }