【发布时间】:2011-07-13 15:05:08
【问题描述】:
我有一个测试程序来演示我希望的最终结果(即使在这个测试程序中这些步骤似乎没有必要)。
程序使用 GZipStream 将数据压缩到文件中。生成的压缩文件是 C:\mydata.dat。
然后我读取这个文件,并将它写入一个新文件。
//Read original file
string compressedFile = String.Empty;
using (StreamReader reader = new StreamReader(@"C:\mydata.dat"))
{
compressedFile = reader.ReadToEnd();
reader.Close();
reader.Dispose();
}
//Write to a new file
using (StreamWriter file = new StreamWriter(@"C:\mynewdata.dat"))
{
file.WriteLine(compressedUserFile);
}
当我尝试解压这两个文件时,原始文件完美解压,但新文件抛出 InvalidDataException 并显示消息 GZip 标头中的幻数不正确。确保您传递的是 GZip 流。
为什么这些文件不同?
【问题讨论】:
-
你为什么用
string而不是byte[]? -
看起来像 File.Copy 的工作(不是?)
-
reader.ReadToEnd() 返回一个字符串
-
另外,我不能使用 File.Copy...我正在读取文件,将其上传到不同的位置,并在那里创建一个新文件...这只是一个简化版本手头的问题
-
@John:您应该一次读取一大块字节。我会发布一个简短的答案
标签: c# file-io gzip filestream gzipstream