fletchsod
fletchsod

Reputation: 3729

GZipStream does gzipping but ungzipping file end up with "Unexpected end of data"

Does anyone know why I'm getting the "Unexpected end of data" error message when un-gzipping the gzip file?

To verify the bytes data is not corrupted, I use the FooTest4.csv to write to file and was able to opened the file successfully.

Both 'FooTest3.csv.gzand 'FooTest2.csv.gz ran into "Unexpected end of data" when un-gzipping.

    public static List<byte> CompressFile(List<byte> parmRawBytes)
    {
        //Initialize variables...
        List<byte> returnModifiedBytes = null;

        File.WriteAllBytes(@"X:\FooTest4.csv", parmRawBytes.ToArray());

        using (var memoryStream = new MemoryStream())
        {
            using (var gzipStream = new GZipStream(memoryStream, CompressionMode.Compress, false))
            {
                gzipStream.Write(parmRawBytes.ToArray(), 0, parmRawBytes.ToArray().Length);
                gzipStream.Flush();
                File.WriteAllBytes(@"X:\FooTest3.csv.gz", memoryStream.ToArray());
                returnModifiedBytes = memoryStream.ToArray().ToList();
            }
        }

        File.WriteAllBytes(@"X:\FooTest2.csv.gz", returnModifiedBytes.ToArray());

        return returnModifiedBytes;
    }

Upvotes: 6

Views: 3290

Answers (2)

kalitsov
kalitsov

Reputation: 1649

GZipStream needs to be closed so it can write some terminating data to the end of the buffer to complete the gzip encoding.

byte[] inputBytes = ...;
using (var compressedStream = new MemoryStream())
{
    using (var compressor = new GZipStream(compressedStream, CompressionMode.Compress))
    {
        compressor.Write(inputBytes, 0, inputBytes.Length);
    }

    // get bytes after the gzip stream is closed 
    File.WriteAllBytes(pathToFile, compressedStream.ToArray());
}

Upvotes: 6

Pau C
Pau C

Reputation: 783

Instead of loading the bytes, compressing and saving them you could do do compression and writing at once. Also I don't know why you're using List<Byte> instead of byte[], maybe this could be it.

void CompressFile(string inputPath, string outputPath)
{
   Stream readStream = new FileStream(inputPath, Filemode.Open);
   Stream writeStream = new FileStream(outputPath, FileMode.Create);
   Stream compressionStream = new GZipStream(writeStream. CompressionMode.Compress);
   byte[] data = new byte[readStream.Length];
   readStream.Read(data, 0, data.Length);
   compressionStream.Write(data, 0, data.Length);
   readStream.Close();
   writeStream.Close();    
}

byte[] CompressFile(string inputPath)
{
  byte[] data = File.ReadAllBytes(inputPath);
  MemoryStream memStream = new MemoryStream(data);
  var gzipStream = new GZipStream(memStream, CompressionMode.Compress);
  gzipStream.Write(data, 0, data.Length);
  gzipStream.Close();
  return gzipStream.ToArray();
}

PS: I wrote the code in the text editor, so there might be errors. Also you say the error is on the "unzippiing", why no show us the unzip code?

Upvotes: 0

Related Questions