0

I'm trying to read a series of gzip files using the code below.

What I end up with is a file that is only a fraction of the total file. Interestingly enough the text file output is almost perfectly truncated at a line break.

public static string UnzipFile(string filename)
{
    FileInfo fi = new FileInfo(filename);
    Console.WriteLine($" - - - - - UnzipFile {filename} {fi.Length} - - - - - ");
    string newFileName = filename.Substring(0, filename.Length - 4);

    try
    {
       byte[] buffer = new byte[4096]; // read in chunks of 4KB
       using (FileStream fs2 = new FileStream(newFileName, FileMode.CreateNew))
       {
          using (FileStream fs = new FileStream(filename, FileMode.Open))
          {
             using (GZipStream zipStream = new GZipStream(fs, CompressionMode.Decompress))
             {
                zipStream.BufferSize = 1024;
                int bytesRead;
                while((bytesRead = zipStream.Read(buffer, 0, buffer.Length)) > 0)
                {
                   Console.WriteLine($" {bytesRead}  ");
                   fs2.Write(buffer, 0, bytesRead);
                   fs2.Flush(true);
                }          
             }           
         }
      }
      FileInfo fi2 = new FileInfo(newFileName);
      Console.WriteLine($" - - - - {newFileName} {fi2.Length}");
      }
      catch (Exception e)
      {
         Console.WriteLine($" - - - - - EXCEPTION {e.Message} {e.StackTrace}");
      }
      return newFileName;
 }

Here is partial output from running the code:

UnzipFile : 177686_2018-10-18_0#289.json.gz 34975
4096
4096
 855
            177686_2018-10-18_0#289.json 9047

So it looks like a file of size 34975 bytes when compressed ends up decompressing only 9047 bytes. If I use gunzip on a mac to expand the file I get a file 223344 bytes in size.

Where have I gone wrong?

edit: Additional data

In case it helps, I get this when using GZip.Decompress

Bad CRC32 in GZIP trailer. (actual(7C64AEAA)!=expected(F641B726)) at Ionic.Zlib.ZlibBaseStream.finish()

Additional data when examining the position of the FileStream

fs pos: 2093 fs len: 33930

0 Answers0