我有一个字符串,我在服务器上Gzip并使用WebClient类下载到客户端。当我尝试解压缩它时,我收到错误信息,表明Magic Number丢失了。我已经尝试过GZipStream类和解决这个问题的ICSharpLib方法,所以我很茫然。

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

以下是代码片段,似乎是有问题的部分:

   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幻数的一部分。

然后,我认为你正在使用错误的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"];

    //...
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top