0

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);
                    }
                }
            }
svick
  • 236,525
  • 50
  • 385
  • 514
Johnv2020
  • 2,088
  • 3
  • 20
  • 36
  • Code looks reasonable. Have you verified that file names are expected (i.e. check for one file with hard-coded name compress ->decompress)? – Alexei Levenkov May 14 '12 at 16:36
  • Sorry not quite sure I follow you, how would the filename affect decompression ? – Johnv2020 May 14 '12 at 16:50
  • I.e. Compression: "Source.txt" -> "compressed.compr", decompression: "random.file" (instead of "compressed.compr") -> "Source.txt" (fails as "random.file" is not compressed at all). – Alexei Levenkov May 14 '12 at 17:00
  • Could you post a short, but complete sample that we could run that shows your problem? – svick May 14 '12 at 17:03

1 Answers1

1

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.

Johnv2020
  • 2,088
  • 3
  • 20
  • 36
  • 1
    This is yet another error in GZipStream. The last four bytes of a gzip stream is the uncompressed (not compressed) length modulo 2^32, as a check on the data. If the low 32 bits of the length of what was uncompressed matches, then the check is good. – Mark Adler May 15 '12 at 03:59
  • I have seen many other problems with GZipStream here on SO. Among them are that it does not properly use fixed vs. dynamic vs. stored blocks resulting in compressed output that is much too large for short input, and crc mismatches are often not detected, resulting in no error when there is a serious error. I would recommend that this class from Microsoft should never be used. – Mark Adler May 15 '12 at 04:04