gpt4 book ai didi

c# - 如何解决 Gzip 魔数(Magic Number)丢失的问题

转载 作者:太空宇宙 更新时间:2023-11-03 19:36:49 26 4
gpt4 key购买 nike

我有一个字符串,我在服务器上进行 Gzip 压缩,然后使用 WebClient 类将其下载到客户端。当我尝试解压缩它时,我收到错误消息,指出缺少魔数(Magic Number)。我已经尝试了 GZipStream 类和 ICSharpLib 方法来解决这个问题,所以我很茫然。

如果我省略通过 WebClient 下载的步骤(使用 DownloadData 将数据返回为 byte[]),压缩/解压就可以工作,所以我只能假设数据被截断或损坏是有问题的,但由于它是压缩数据,我不确定如何调试它。

这是似乎有问题的部分代码片段:

   byte[] response
try {
response = client.DownloadData(Constants.GetSetting("SyncServer"));
} catch {
MessageBox.Show("There was a problem synchronizing the data. Please try verify the supplied credentials or try again later.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}

int rows = SQLiteAPI.ImportStatHistoryXML(CurrentUser.User, myCampus, Convert.ToBase64String(response));

public static int ImportStatHistoryXML(Person tempPerson, Campus tempCampus, string xmlFile) {
byte[] encryptedFile = Convert.FromBase64String(xmlFile);
MemoryStream memStream = new MemoryStream(encryptedFile);
memStream.ReadByte();
GZipInputStream stream = new GZipInputStream(memStream);
MemoryStream memory = new MemoryStream();
byte[] writeData = new byte[4096];
int size;

while (true) {
size = stream.Read(writeData, 0, writeData.Length);
if (size > 0) {
memory.Write(writeData, 0, size);
} else {
break;
}
}
stream.Close();
memory.Position = 0;
StreamReader sr = new StreamReader(memory);
string decompressed = sr.ReadToEnd();
DataSet tempSet = new DataSet();
StringReader xmlReader = new StringReader(decompressed);
tempSet.ReadXml(xmlReader);
DataTable statTable = tempSet.Tables["Stats"];
...more unrelated processing of the table
}

如有任何帮助,我们将不胜感激。附言我正在使用 Base64 字符串来在网络上来回传递。这实际上可能是我搞砸的领域,因为我以前没有在桌面应用程序和 Web 服务之间进行过 Web 请求和响应。

最佳答案

首先,我认为该代码段无效,因为 DownloadString 返回(如预期)一个字符串。

现在,我是否正确理解它在您使用 DownloadData 时工作正常而在您使用 DownloadString 时工作不正确?这是有道理的,因为将 Gzip 数据解码为 Unicode 是无效的。

编辑:

好的,ToBase64String 和 FromBase64String 应该没问题。但是如果能避开,直接传byte[]就好了。

public static int ImportStatHistoryXML(Person tempPerson, Campus tempCampus, byte[] compressedFile) {

然后你将去掉函数的第一行(从 base64 解码)。请注意,我们正在将 encryptedFile 重命名为 compressedFile。

行:

memStream.ReadByte();

不应该在那里。您正在读取一个字节并将其丢弃。如果一切如我们所料,字节为 0x1F,即 gzip 魔数(Magic Number)的一部分。

然后,我认为您使用了错误的 gzip 类。你要GZipStream .它的构造如下:

GZipStream stream = new GZipStream(memStream, CompressionMode.Decompress);

然后,直接在上面使用 StreamReader:

StreamReader sr = new StreamReader(stream);

如果您知道编码会有所帮助,但希望默认值是正确的。然后从那里看起来是正确的。所以,总的来说,我们得到以下结果。

public static int ImportStatHistoryXML(Person tempPerson, Campus tempCampus, byte[] compressedFile) {
MemoryStream memStream = new MemoryStream(compressedFile);
GZipStream gzStream = new GZipStream(memStream, CompressionMode.Decompress);
StreamReader sr = new StreamReader(gzStream);
string decompressed = sr.ReadToEnd();
DataSet tempSet = new DataSet();
StringReader xmlReader = new StringReader(decompressed);
tempSet.ReadXml(xmlReader);
DataTable statTable = tempSet.Tables["Stats"];

//...
}

关于c# - 如何解决 Gzip 魔数(Magic Number)丢失的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1025777/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com