原始流有数据,Deflate 返回零字节

5 .net c# zip stream deflatestream

我正在读取数据(碰巧是一个 adCenter 报告),该数据应该是压缩的。使用普通流读取内容,我得到了几千字节的乱码,所以这似乎是合理的。所以我将流提供给 DeflateStream。

首先,它报告“块长度与其补码不匹配”。简短的搜索表明存在一个双字节前缀,实际上,如果我在打开 DeflateStream 之前调用 ReadByte() 两次,异常就会消失。

但是,DeflateStream 现在根本不返回任何内容。我花了整个下午的大部分时间来寻找线索,但没有运气。帮助我,StackOverflow,你是我唯一的希望!谁能告诉我我错过了什么?

这是代码。当然,在测试时我一次只启用两个注释块之一。

_results = new List<string[]>();
using (Stream compressed = response.GetResponseStream())
  {
  // Skip the zlib prefix, which conflicts with the deflate specification
  compressed.ReadByte();  compressed.ReadByte();

  // Reports reading 3,000-odd bytes, followed by random characters
  /*byte[]  buffer    = new byte[4096];
  int     bytesRead = compressed.Read(buffer, 0, 4096);
  Console.WriteLine("Read {0} bytes.", bytesRead.ToString("#,##0"));
  string  content   = Encoding.ASCII.GetString(buffer, 0, bytesRead);
  Console.WriteLine(content);*/

  using (DeflateStream decompressed = new DeflateStream(compressed, CompressionMode.Decompress))
    {
    // Reports reading 0 bytes, and no output
    /*byte[]  buffer    = new byte[4096];
    int     bytesRead = decompressed.Read(buffer, 0, 4096);
    Console.WriteLine("Read {0} bytes.", bytesRead.ToString("#,##0"));
    string  content   = Encoding.ASCII.GetString(buffer, 0, bytesRead);
    Console.WriteLine(content);*/

    using (StreamReader reader = new StreamReader(decompressed))
      while (reader.EndOfStream == false)
        _results.Add(reader.ReadLine().Split('\t'));
    }
  }
Run Code Online (Sandbox Code Playgroud)

您可能从最后一行猜到,解压后的内容应该是 TDT。

只是为了好玩,我尝试使用 GZipStream 解压缩,但它报告幻数不正确。MS 的文档只是说“下载的报告是使用 zip 压缩进行压缩的。您必须先解压缩报告,然后才能使用其内容。”


这是最终起作用的代码。我必须将内容保存到文件中,然后将其读回。这似乎不合理,但对于我正在处理的少量数据来说,这是可以接受的,我接受它!

WebRequest   request  = HttpWebRequest.Create(reportURL);
WebResponse  response = request.GetResponse();

_results = new List<string[]>();
using (Stream compressed = response.GetResponseStream())
  {
  // Save the content to a temporary location
  string  zipFilePath = @"\\Server\Folder\adCenter\Temp.zip";
  using (StreamWriter file = new StreamWriter(zipFilePath))
    {
    compressed.CopyTo(file.BaseStream);
    file.Flush();
    }

  // Get the first file from the temporary zip
  ZipFile  zipFile = ZipFile.Read(zipFilePath);
  if (zipFile.Entries.Count > 1)  throw new ApplicationException("Found " + zipFile.Entries.Count.ToString("#,##0") + " entries in the report; expected 1.");
  ZipEntry  report = zipFile[0];

  // Extract the data
  using (MemoryStream decompressed = new MemoryStream())
    {
    report.Extract(decompressed);
    decompressed.Position = 0;  // Note that the stream does NOT start at the beginning
    using (StreamReader reader = new StreamReader(decompressed))
      while (reader.EndOfStream == false)
        _results.Add(reader.ReadLine().Split('\t'));
    }
  }
Run Code Online (Sandbox Code Playgroud)

Fox*_*ire 1

您会发现 DeflateStream 在解压缩的数据方面受到极大限制。事实上,如果您期望完整的文件,那么它根本没有用。ZIP 文件有数百种(大部分是较小的)变体,DeflateStream 只能处理其中的两到三个。

最好的方法可能是使用专用的库来读取 Zip 文件/流,例如DotNetZip或 SharpZipLib(有些不维护)。