【发布时间】:2012-06-30 18:40:27
【问题描述】:
大家好,我遇到了这个异常:
The magic number in GZip header is not correct. Make sure you are passing in a GZip stream.
我所做的只是将文件解压缩为 GZip,然后将其传递到流中,以将其传递到文件中。
using (FileStream fInStream = new FileStream(@"C:\Users\UNI\Desktop\FrostbyteDBUpdateProgram\FrostbyteDBUpdateProgram\bin\Debug\" + fileName, FileMode.Open, FileAccess.Read))
{
using (GZipStream gZip = new GZipStream(fInStream, CompressionMode.Decompress))
{
using (FileStream fOutStream = new FileStream(@"C:\Users\UNI\Desktop\FrostbyteDBUpdateProgram\FrostbyteDBUpdateProgram\bin\Debug\test1.docx", FileMode.Create, FileAccess.Write))
{
byte[] tempBytes = new byte[4096];
int i;
while ((i = gZip.Read(tempBytes, 0, tempBytes.Length)) != 0)
{
fOutStream.Write(tempBytes, 0, i);
}
}
gZip.Close();
}
fInStream.Close();
}
【问题讨论】:
-
问:您确认您打开的文件流的文件名是正确的吗?问:您是否打开该文件以确认它是一个有效的 .zip 文件?问:为什么是“4096”?
-
旁注:无需致电
Close(因为您正确使用using)并考虑使用Stream.CopyTo而不是手动复制流,因为您已经可以拥有4.0。 -
你将如何实现stream.copyTo?
标签: c#