Unzipping and reading a .gz (Gzip file) in C#

Ankit

Member
Joined
Dec 16, 2020
Messages
17
Programming Experience
3-5
Below is the method where I am reading a csv file from an azure blob container and later calling a function to copy the contents in a tabular storage.
Now my requirement has bit changed and now .csv file will be compressed to .gz file in the blob container. I would like to know, how can I modify the below code so that I can read .gz file , decompress it and then pass the contents as I am already passing
C#:
public async Task<string> ReadStream(string BlobcontainerName, string fileName, string connectionString)
        {
            
            var contents = await DownloadBlob(BlobcontainerName, fileName, connectionString);
            string data = Encoding.UTF8.GetString(contents.ToArray());
            return data;
        }

foreach (var files in recFiles)// recFiles are list of CSV files
            {
                string data = await ReadStream(containerName, files.Name, connectionString);}
c#
 
In that code, what type is contents? Basically, you need to have your data exposed via a Stream of some sort and you then put a GZipStream on top of that, then you read the data from that like you would any other Stream. For example, if contents is a MemoryStream:
C#:
using (var unzipper = new GZipStream(contents, CompressionLevel.Optimal))
{
    // Use unzipper here.
}
 
In that code, what type is contents? Basically, you need to have your data exposed via a Stream of some sort and you then put a GZipStream on top of that, then you read the data from that like you would any other Stream. For example, if contents is a MemoryStream:
C#:
using (var unzipper = new GZipStream(contents, CompressionLevel.Optimal))
{
    // Use unzipper here.
}
Content is Memory Stream and filename will be test1.csv.gz, In //Use Unzipper here I should be providing Decompress(fileName) ??
 
In //Use Unzipper here I should be providing Decompress(fileName) ??
There is no such method. You should read the documentation for the GZipStream class. It works the same as any other Stream from the outside.
 
There is no such method. You should read the documentation for the GZipStream class. It works the same as any other Stream from the outside.
I see here what you mean, now question is I see Unzipper has Read method which is close to what I am passing in my "data" variable? but i dont see that o be working if i pass like ths

C#:
using (var unzipper = new GZipStream(contents, CompressionMode.Decompress))
            {
                //unzipper.ReadByte();
                string data = Encoding.UTF8.GetString(unzipper.ReadByte().ToArray());
            }
 
If you have read the documentation then you know that ReadByte reads one byte. Is that what you want to do? Obviously not. Do some research and learn how to read data from a Stream. It doesn't matter what type of Stream it is as they are all Streams, e.g. reading from a FileStream is the same as reading from a GZipStream.
 
Using CompressionMode.Decompress as second parameter is correct, CompressionLevel is for compression.
Also since this is string content you can use StreamReader to read from GZipStream, it has the convenient ReadToEnd method (or ReadToEndAsync).
 
I wrote my code something like this and I was able to read a .gz file while writing a test case. In Production however I am getting error as "Stream does not support reading. (Parameter 'stream')" on line I am using GZip. What wrong am i doing now??


C#:
public async Task<string> ReadStream(string containerName, string digestFileName,string fileName, string connectionString)
        {
            string data = string.Empty;
            string fileExtension = Path.GetExtension(fileName);
            var contents = await DownloadBlob(containerName, digestFileName, connectionString);
            if (fileExtension == ".gz")
            {
//Error on this line
                using (var unzipper = new GZipStream(contents, CompressionMode.Decompress))
                {
                    using (StreamReader reader = new StreamReader(unzipper, Encoding.UTF8))
                    {
                        data = reader.ReadToEnd();
                    }

                }
            }
            else
            {
               data = Encoding.UTF8.GetString(contents.ToArray());
            }
            return data;
        }
 
Are you sure the stream contains a valid compressed data stream?
 
Back
Top Bottom