UnZipFile
方法将数据从inputStream
写入outputWriter
。
为什么 sr.ToString()
返回 System.Byte[]
而不是数据?
using (var sr = new StringWriter())
{
UnZipFile(response.GetResponseStream(), sr);
var content = sr.ToString();
}
public static void UnZipFile(Stream inputStream, TextWriter outputWriter)
{
using (var zipStream = new ZipInputStream(inputStream))
{
ZipEntry currentEntry;
if ((currentEntry = zipStream.GetNextEntry()) != null)
{
var size = 2048;
var data = new byte[size];
while (true)
{
size = zipStream.Read(data, 0, size);
if (size > 0)
{
outputWriter.Write(data);
}
else
{
break;
}
}
}
}
}
最佳答案
问题出在线上:
outputWriter.Write(data);
StringWriter.Write
没有期望 byte[]
的重载。因此,将调用 Write(Object)
。并根据MSDN :
Writes the text representation of an object to the text string or stream by calling the ToString method on that object.
在字节数组上调用 ToString
返回 System.byte[]
,解释如何在 StringWriter
中获取该字符串。
关于c# - 为什么 StringWriter.ToString 返回 `System.Byte[]` 而不是数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12354558/