0

我正在使用以下 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 文件。

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

4

2 回答 2

1

ok 发现问题:

第 2 行必须如下所示:

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

于 2011-04-06T15:41:26.997 回答
0

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

如果你真的想创建 zip 档案,你可能想使用像 SharpZipLib 这样的库:http: //www.icharpcode.net/opensource/sharpziplib/

于 2012-03-10T14:08:32.193 回答