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

  • Question

  • User1898849233 posted

    Hi,

    While comperessing, it wirks fine, but while decompressing the compressed sting using GZip, I am getting error "The magic number in GZip header is not correct. Make sure you are passing in a GZip stream."  Please go through the follwoing code.

    string Body = string.Empty;

    string strZiped= Zip(Body);
    string strUnZiped = UnZip(strZiped);

    public static string Zip(string value)
            {
                byte[] byteArray = new byte[value.Length];
                int indexBA = 0;
                foreach (char item in value.ToCharArray())
                {
                    byteArray[indexBA++] = (byte)item;
                }

                //Prepare for compress
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                System.IO.Compression.GZipStream sw = new System.IO.Compression.GZipStream(ms,
                System.IO.Compression.CompressionMode.Compress);
                sw.Write(byteArray, 0, byteArray.Length);
                sw.Close();
                byteArray = ms.ToArray();
                System.Text.StringBuilder sB = new System.Text.StringBuilder(byteArray.Length);
                foreach (byte item in byteArray)
                {
                    sB.Append((char)item);
                }
                ms.Close();
                sw.Dispose();
                ms.Dispose();
                string strCompressed = sB.ToString();
                return strCompressed;
            }


    public static string  UnZip(string Compressedvalue)
            {
                byte[] byteArray = Encoding.UTF8.GetBytes(Compressedvalue);
                MemoryStream stream = new MemoryStream(byteArray);
                Stream sourceStream = stream;
                if (sourceStream == null)
                {
                    throw new ArgumentException();
                }

                if (!sourceStream.CanRead)
                {
                    throw new ArgumentException();
                }

                MemoryStream memoryStream = new MemoryStream();
                const int bufferSize = 65536;

                using (GZipStream gzipStream = new GZipStream(sourceStream, CompressionMode.Decompress))
                {
                    byte[] buffer = new byte[bufferSize];

                    int bytesRead = 0;

                    do
                    {
                        bytesRead = gzipStream.Read(buffer, 0, bufferSize);
                    }
                    while (bytesRead == bufferSize);

                    memoryStream.Write(buffer, 0, bytesRead);
                }
                byte[] btArraymemoryStream = memoryStream.ToArray();
                string st = Encoding.UTF8.GetString(btArraymemoryStream);
                return st;

            }

    Monday, August 5, 2013 2:53 AM

All replies

  • User-1662538993 posted

       MemoryStream stream = new MemoryStream(byteArray);

    Close memory stream and then try it.

    Monday, August 5, 2013 3:46 PM