GZipStream giving message "The magic number in GZip header is notcorrect"

S

stewart

I'm getting the following error when trying to decompress a GZip
encoded string. Below is a simplified version of the code which you
can run.

// Define a string "test" which we are going to compress
then decompress
byte[] b = ASCIIEncoding.UTF8.GetBytes("test");

// Read in b and compress it to string s
MemoryStream ms = new MemoryStream();
GZipStream gz = new GZipStream
(ms ,CompressionMode.Compress);
gz.Write(b, 0, b.Length);
ms.Position = 0;
StreamReader sr = new StreamReader(ms);
string s = sr.ReadToEnd();
gz.Close();

// Take compressed string s and extract it to byte array
b2
byte[] b2 = new byte[4096];
ms = new MemoryStream(ASCIIEncoding.UTF8.GetBytes(s));
gz = new GZipStream(ms, CompressionMode.Decompress);
gz.Read(b2, 0, b2.Length);

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

Any thoughts?
 
H

hack2root

I'm getting the following error when trying to decompress a GZip
encoded string.  Below is a simplified version of the code which you
can run.

            // Define a string "test" which we are going to compress
then decompress
            byte[] b = ASCIIEncoding.UTF8.GetBytes("test");

            // Read in b and compress it to string s
            MemoryStream ms = new MemoryStream();
            GZipStream gz = new GZipStream
(ms ,CompressionMode.Compress);
            gz.Write(b, 0, b.Length);
            ms.Position = 0;
            StreamReader sr = new StreamReader(ms);
            string s = sr.ReadToEnd();
            gz.Close();

            // Take compressed string s and extract it to byte array
b2
            byte[] b2 = new byte[4096];
            ms = new MemoryStream(ASCIIEncoding.UTF8.GetBytes(s));
            gz = new GZipStream(ms, CompressionMode.Decompress);
            gz.Read(b2, 0, b2.Length);

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

Any thoughts?


You doing thigs in a wrong way. To be precize, absolutely mirrrered in
comparison of right sequence.

You did:

GetBytes*->Compress->GetBytes*->Decompress

Error in (*)

See my code:

// compressor
private static byte[] Compress(byte[] bytes)
{
using (MemoryStream ms = new MemoryStream())
{
using (GZipStream gs = new GZipStream(ms,
CompressionMode.Compress, true))
{
gs.Write(bytes, 0, bytes.Length);
}
ms.Position = 0L;
return ToByteArray(ms);
}
}

// decompressor
private static byte[] Decompress(byte[] bytes)
{
byte[] result;
using (MemoryStream ms = new MemoryStream())
{
ms.Write(bytes, 0, bytes.Length);
using (GZipStream gs = new GZipStream(ms,
CompressionMode.Decompress, true))
{
ms.Position = 0L;
result = ToByteArray(gs);
}
}
return result;
}


// streaming to byte array
private static byte[] ToByteArray(Stream stream)
{
int count = 0;
List<byte> result = new List<byte>();
try
{
byte[] buffer = new byte[0x20000];
int bytes = 0;
while ((bytes = stream.Read(buffer, 0, 0x20000)) > 0)
{
count += bytes;
for (int i = 0; i < bytes; i++)
{
result.Add(buffer);
}
}
}
catch (Exception ex)
{
//LogHelper.WriteErrorSync(ex);
return null;
}
//LogHelper.Write(string.Format
(CultureInfo.InvariantCulture, "streaming data: {0} bytes", new object
[] { count }));
return result.ToArray();
}



Examples:

// i used additional encoding to base64, you probably not ;)

// compress in strinct order
string obj = Convert.ToBase64String(Compress(Encoding.UTF8.GetBytes
(sb.ToString())));

// decompress in reverse order
string text = Encoding.UTF8.GetString(Decompress
(Convert.FromBase64String(data)));
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top