11

我正在使用以下代码压缩文件,它工作正常,但是当我使用 WinRar 解压缩时,我得到没有扩展名的原始文件名,任何线索为什么如果文件名是myReport.xls我解压缩时我只得到myReport

using (var fs = new FileStream(fileName, FileMode.Open))
{
    byte[] input = new byte[fs.Length];
    fs.Read(input, 0, input.Length);
    fs.Close();

    using (var fsOutput = new FileStream(zipName, FileMode.Create, FileAccess.Write))
    using(var zip = new GZipStream(fsOutput, CompressionMode.Compress))
    {
        zip.Write(input, 0, input.Length);
        zip.Close();
        fsOutput.Close();
    }
}
4

2 回答 2

25

GZip 只压缩一个文件 - 不知道名称。因此,如果您压缩文件myReport.xls,您应该命名它myReport.xls.gz。解压缩时,最后一个文件扩展名将被删除,因此您最终会得到原始文件名。

这就是它多年来在 Unix/Linux 中的使用方式......

于 2011-10-14T11:28:03.480 回答
2

确实很奇怪。一个简短的搜索得出以下结论:

http://dotnetzip.codeplex.com/discussions/268293

这表示 GZipStream 无法知道正在写入的流的名称,并建议您FileName直接设置该属性。

希望有帮助。

于 2011-10-14T11:02:17.027 回答