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

Mohd Zubair picture Mohd Zubair · Oct 28, 2012 · Viewed 7.8k times · Source

I want to extract a exe file. The exe file contain some files and folders. When I try to extract the file using winrar it gets extracted but when I am trying to extract the exe file using some examples I am getting this error:

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

I have used some samples and googled a lot for my problem but didn't get my answer, and I have used some libraries also.

I used this code but same error:

    public static void Decompress(FileInfo fi)
    {
        // Get the stream of the source file.
        using (FileStream inFile = fi.OpenRead())
        {
            // Get original file extension, for example
            // "doc" from report.doc.gz.
            string curFile = fi.FullName;
            string origName = curFile.Remove(curFile.Length -
                    fi.Extension.Length);

            //Create the decompressed file.
            using (FileStream outFile = File.Create(origName))
            {
                using (GZipStream Decompress = new GZipStream(inFile,
                        CompressionMode.Decompress))
                {
                    // Copy the decompression stream 
                    // into the output file.
                    Decompress.CopyTo(outFile);

                    Console.WriteLine("Decompressed: {0}", fi.Name);

                }
            }
        }
    }

Answer

Yannick Blondeau picture Yannick Blondeau · Oct 29, 2012

That's because the .exe file is a self-extracting archive...

You should give DotNetZip a try. From the project's FAQ:

Does this library read self-extracting zip files?

Yes. DotNetZip can read self-extracting archives (SFX) generated by WinZip, and WinZip can read SFX files generated by DotNetZip.

You can install it from Nuget easily.