Search code examples
c#gzipgzipstream

The stream size in GZip footer does not match the real stream size


I'm getting an IO exception "The stream size in GZip footer does not match the real stream size" when decompressing from GzipStream. This error is occurring 100% of the time on multiple files so I don't believe that this is a "real" corrupt file issue.

The compression code is as follows:

 using (var fileStream = fileInfo.OpenRead())
            {
                using (var outFile = File.Create(Path.Combine(backupLocation, backupFileName.ToString())))
                {
                    using (var gzCompressionStream = new GZipStream(outFile, CompressionMode.Compress))
                    {
                        fileStream.CopyTo(gzCompressionStream);
                    }
                }
            }

The decompression code which is throwing the exception is as follows:

using (var fileStream = fileInfo.OpenRead())
            {
                // remove the extension
                var fileName = fileInfo.Name;
                var originalName = fileName.Remove(fileName.Length - fileInfo.Extension.Length);

                using (var outFile = File.Create(Path.Combine(transferLocation, originalName)))
                {
                    using (var gzDecompressionStream = new GZipStream(fileStream,CompressionMode.Decompress))
                    {
                        gzDecompressionStream.CopyTo(outFile);
                    }
                }
            }

Solution

  • All, thanks for your help - looks like I've found the problem. I'm only getting an error when the compressed file size is greater than 4GB, below this everything works fine, - this shouldn't be a problem as MSDN states that GZipStream works for file sizes up to 8GB with .Net 4 (which I'm using) and the maximum file size will always be below 6GB (application limit). Previous versions of GZipStream only supported up to 4GB however - it looks as if the MSDN documentation is incorrect in this case.