我正在使用以下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。不仅仅是.xml文件,.txt文件也发生了同样的事情。
你能帮我看看我哪里做错了吗?
发布于 2011-04-06 23:41:27
ok找到了问题:
第二行必须如下所示:
File.Create(@"C:\Projects\Samples\test\compressed.xml.zip",fs = FileStream 0);
发布于 2012-03-10 22:08:32
GZipStream不会创建压缩归档。它创建一个gzip文件,其中只包含一个文件,并且根本不需要存储文件名。通常,您应该使用.gz扩展名来标识gzip文件,并且通常使用原始文件的全名,并在文件末尾附加.gz。有关gzip格式的更多信息,请参阅此处:http://en.wikipedia.org/wiki/Gzip#File_format
如果您实际上想要创建压缩归档文件,则可能需要使用类似SharpZipLib:http://www.icsharpcode.net/opensource/sharpziplib/的库
https://stackoverflow.com/questions/5564899
复制相似问题