【问题标题】:GZipStream not workingGZipStream 不工作
【发布时间】:2011-04-06 10:30:33
【问题描述】:

我正在使用以下 C# 代码来压缩文件:

// Open the stream we want to compress
FileStream fs = File.Create(@"C:\Projects\Samples\test\compressed.zip", 0);

// Creates the GZipStream
GZipStream gzip = new GZipStream(fs, CompressionMode.Compress);

// Reading the content to compress
 byte[] bytes = File.ReadAllBytes(@"C:\Projects\Samples\samplefile.xml");

// Writing compressed content
gzip.Write(bytes, 0, bytes.Length);
gzip.Close();  // This also closes the FileStream (the underlying stream)

但是,当我从 Windows 资源管理器中提取文件时,该文件会丢失其扩展名,因此它不再是 samplefile.xml,而是变成了 samplefile。 .txt 文件发生了同样的事情,而不仅仅是 .xml 文件。

你能帮我看看我做错了什么吗?

【问题讨论】:

    标签: c# gzipstream


    【解决方案1】:

    好的,找到问题了:

    第 2 行必须如下:

    FileStream fs = File.Create(@"C:\Projects\Samples\test\compressed.xml.zip", 0);

    【讨论】:

      【解决方案2】:

      GZipStream 不会创建 zip 存档。它创建一个 gzip 文件,该文件仅包含一个文件,并且根本不需要存储文件名。通常您应该使用.gz 扩展名来标识gzip 文件,并且通常使用原始文件的全名并在末尾附加.gz。有关 gzip 格式的更多信息,另请参阅此处:http://en.wikipedia.org/wiki/Gzip#File_format

      如果您真的想创建 zip 存档,您可能需要使用 SharpZipLib 之类的库:http://www.icsharpcode.net/opensource/sharpziplib/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-09
        相关资源
        最近更新 更多