CodeKicks.com
Focus on Microsoft Technologies - Tutorials, Articles, Code Samples.

Friday, April 03, 2009

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

When a file is compressed and uncompressed in C#.net using GZip stream. Everything works fine. But if you try to uncompress a file which is compressed through Zip utility.

It will throw an error saying The magic number in GZip header is not correct. Make sure you are passing in a GZip stream.

This is due to basic algorithm used to generate the header information in zip file. This error can be solved using the command line utility of winzip.

Steps to do:

1. Create a "UnCompress.bat" file.

2. Place the following code in this bat file.

@echo off

wzunzip -d "C:\Test.zip" "C:\Test"

@echo off

pause

Note : wzunzip -d "C:\Test.zip" "C:\Test"

-d is for uncompressing using winzip utility

"C:\Test.zip" is the source file to be uncompressed

"C:\Test" is the destination location.

wzunzip is the exe that ships along with winzip providing uncompression

wzzip is the exe that ships along with winzip providing compression

Run the "bat" file from C#.Net code using this:

System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo(); info.UseShellExecute = true;

info.FileName = @"C:\UnCompress.bat";

System.Diagnostics.Process.Start(info);

Post a Comment

Nick said...

Thanks for providing the solution.

-Nick.

Anonymous said...

I was trying to do this via C# code and was encountering this error because I was first converting the byte[] to a UTF8 string and then back to byte[].

If you are doing this, you get back the wrong byte array. Keep the original byte[] in tact and you should be good to go.