Vasa Serafin
Vasa Serafin

Reputation: 306

Exception using GZip

Hello everyone I am getting this exception:

The magic number in GZip header is not correct. Make sure you are passing in a GZip stream.

All I have done is decompress the file into a GZip and then pass that into a stream, to pass that into a file.

using (FileStream fInStream = new FileStream(@"C:\Users\UNI\Desktop\FrostbyteDBUpdateProgram\FrostbyteDBUpdateProgram\bin\Debug\" + fileName, FileMode.Open, FileAccess.Read))
        {
            using (GZipStream gZip = new GZipStream(fInStream, CompressionMode.Decompress))
            {
                using (FileStream fOutStream = new FileStream(@"C:\Users\UNI\Desktop\FrostbyteDBUpdateProgram\FrostbyteDBUpdateProgram\bin\Debug\test1.docx", FileMode.Create, FileAccess.Write))
                {
                    byte[] tempBytes = new byte[4096];
                    int i;
                    while ((i = gZip.Read(tempBytes, 0, tempBytes.Length)) != 0)
                    {
                        fOutStream.Write(tempBytes, 0, i);
                    }
                }
                gZip.Close();
            }
            fInStream.Close();
        }

Upvotes: 0

Views: 3206

Answers (1)

Hans Passant
Hans Passant

Reputation: 941545

The GZipStream class is not suitable to read compressed archives in the .gz or .zip format. It only knows how to de/compress data, it doesn't know anything about the archive file structure. Which can contain multiple files, note how the class doesn't have any way to select the specific file in the archive you want to decompress.

Zip archive support will be added in .NET 4.5. Until then you could use the popular SharpZipLib or DotNetZip libraries. Google their name to find the download.

If the file you want to decompress was actually generated by GZipStream then there's a bug in the code that did that, we can't see it.

Upvotes: 3

Related Questions