错误:gzip头的幻数不正确不正确、错误、gzip

2023-09-06 14:19:50 作者:梦九

我有两种方法,一种是我使用的图像转换为Base64的字符串,这样我就可以将其存储在XML标签,另一个为Base64的字符串转换回图像。我能够将图像转换为字符串,并将其存储在XML,但是当我试图将字符串转换回的图像,我发现了以下错误:在gzip头的幻数是不正确的制作。确保你传递一个GZip压缩数据流。

在如何解决这个有什么想法?

 公共静态字符串ConvertToBase64String(图片图片,格式的imageformat)
{
    MemoryStream的流=新的MemoryStream();
    Image.Save(流格式);

    byte []的imageBytes = stream.ToArray();

    MemoryStream的memStream =新的MemoryStream();
    GZipStream zipStream =新GZipStream(memStream,COM pressionMode.Com preSS);
    zipStream.Write(imageBytes,0,imageBytes.Length);

    字符串和imagestring = Convert.ToBase64String(imageBytes);

    stream.Close();
    memStream.Close();

    返回和imagestring;
}

公共静态图像Base64StringToImage(字符串ImageArray)
{
    byte []的base64String = Convert.FromBase64String(ImageArray);

    MemoryStream的memStream =新的MemoryStream(base64String);
    GZipStream zipStream =新GZipStream(memStream,COM pressionMode.Decom preSS);
    zipStream.Read(base64String,0,base64String.Length);

    ImageConverter IC =新ImageConverter();
    形象画像=(图)ic.ConvertFrom(base64String);

    memStream.Close();

    返回形象;
}
 

解决方案

我看到code几个误区。

fiddler抓iPhone手机提示 gzip头中的幻数不正确,请确保正在传入gzip流 ,请问如何解决

这导致该错误消息的问题是,你没有转换为基64字符串不压缩的数据( memStream.ToArray()),但你写的压缩数据流中的数据( imageBytes )。当您尝试解压缩未压缩的数据,你会得到错误信息。

另一个主要问题是,你只是读取从压缩流中的数据的一部分,因为你使用的是压缩数据的大小尺寸多少从压缩流中读取。

另外,你忽略了从方法的结果。它返回字节确实处于阵列中,它可以是大于所请求的字节的数量少的数量。由于方法没有返回的全部信息,你必须循环,直到你实际上已经得到了来自流中的所有数据。该方法将返回为零时,有没有更多的数据读取。

另外一个问题是,你写你使用的后端,你从读取存储数据流的阵列。由于解压缩的数据通常比压缩数据时,你会被覆盖的数据的速度比你可以读取它。然而,随着解压缩后的数据将不适合的阵中,你不能用它反正。

I have two methods, one that I use to convert an image to a Base64 string so I can store it in an XML tag and another to convert the Base64 string back to an image. I'm able to convert the image to a string and store it in the XML, but when I try to convert the string back to an image I'm getting the following error: "The magic number in GZip header is not correct. Make sure you are passing in a GZip stream."

Any thoughts on how to resolve this?

public static string ConvertToBase64String(Image Image, ImageFormat Format)
{
    MemoryStream stream = new MemoryStream();
    Image.Save(stream, Format);

    byte[] imageBytes = stream.ToArray();

    MemoryStream memStream = new MemoryStream();
    GZipStream zipStream = new GZipStream(memStream, CompressionMode.Compress);
    zipStream.Write(imageBytes, 0, imageBytes.Length);

    string imageString = Convert.ToBase64String(imageBytes);

    stream.Close();
    memStream.Close();

    return imageString;
}

public static Image Base64StringToImage(string ImageArray)
{
    byte[] base64String = Convert.FromBase64String(ImageArray);

    MemoryStream memStream = new MemoryStream(base64String);
    GZipStream zipStream = new GZipStream(memStream, CompressionMode.Decompress);
    zipStream.Read(base64String, 0, base64String.Length);

    ImageConverter ic = new ImageConverter();
    Image image = (Image)ic.ConvertFrom(base64String);

    memStream.Close();

    return image;
}

解决方案

I see several errors in the code.

The problem that is causing the error message is that what you are not converting to a base 64 string is not the zipped data (memStream.ToArray()), but the data that you wrote to the zip stream (imageBytes). When you try to unzip the data that is not zipped, you get the error message.

Another major problem is that you are only reading part of the data from the zip stream, as you are using the size of the zipped data as size for how much to read from the zip stream.

Also, you are ignoring the result from the Read method. It returns the number of bytes that was actually placed in the array, which can be smaller than the number of bytes requested. As the Read method doesn't have to return all available data, you have to loop until you have actually got all data from the stream. The Read method will return zero when there is no more data to read.

Another problem is that you are writing to the array that you are using as back end for the memory stream that you are reading from. As the unzipped data generally is larger than the zipped data, you will be overwriting the data faster than you can read it. However, as the unzipped data won't fit in the array, you can't use it for that anyway.