我正在阅读应该压缩的数据(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'));
}
}
正如您可能从最后一行猜到的那样,解压缩的内容应该是 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'));
}
}