How can I used ReadAllLines with gzipped file

555 Views Asked by At

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?

3

There are 3 best solutions below

3
On BEST ANSWER

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();
            }
        }
    }
}
1
On

There is no such thing built-in. You'll have to write yourself a small utility function.

2
On

You'd have to inflate the file first as the algorithm for gzip deals with byte data not text and incorporates a CRC. This should work for you: EDIT - I cant comment for some reason, so this if for the bytestocompress question


byte[] decompressedBytes = new byte[4096];
using (FileStream fileToDecompress = File.Open("C:\myfile.gz", FileMode.Open))
{
    using (GZipStream decompressionStream = new GZipStream(fileToDecompress, CompressionMode.Decompress))
    {
        decompressionStream.Read(decompressedBytes, 0, bytesToCompress.Length);
    }
}
var pnDates = System.Text.Encoding.UTF8.GetString(decompressedBytes);