Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# GZipStream工作,但扩展丢失_C#_.net_Compression_Gzipstream - Fatal编程技术网

C# GZipStream工作,但扩展丢失

C# GZipStream工作,但扩展丢失,c#,.net,compression,gzipstream,C#,.net,Compression,Gzipstream,我用下面的代码来压缩一个文件,它工作得很好,但是当我用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(); us

我用下面的代码来压缩一个文件,它工作得很好,但是当我用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();
    }
}

真的很奇怪。经过简短搜索,得出以下结论:

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


希望有帮助。

GZip只压缩一个文件,而不知道文件名。因此,如果压缩文件
myReport.xls
,则应将其命名为
myReport.xls.gz
。解压时,最后一个文件扩展名将被删除,因此您将以原始文件名结束


这就是它在Unix/Linux中长期使用的方式…

AFAIK GZip对文件或文件夹一无所知。。。OtohZip与之非常不同,因为它具有所有这些功能,包括文件属性等。。。也许您想使用Zip库?注意,GZip是一种压缩格式,而Zip是一种压缩存档格式。(TAR是存档格式的一个示例。)不,它不起作用,因为zip对象没有要分配的名称或文件名属性。已尝试使用.NET 3.5和.NET 4,但没有差异…:(@Davide Piras但如果您使用DotNetZip,它有一个方法
AddFile
-这会获取文件名并存储它…GZip与ZIP有些不同,尽管名称非常相似…谢谢,这就是问题所在,现在只需添加扩展名而不是用.ZIP替换它即可解决。