0

我正在使用以下代码序列化一个对象,该代码使用 GZip 和 Xml:

        FileStream fs = new FileStream(destinationfolder + "/myFileName.gz",
                                       FileMode.Create, FileAccess.Write);
        using (var gz = new GZipStream(fs, CompressionMode.Compress)) {
            var serializer = new XmlSerializer(typeof(MyObjectType));
            serializer.Serialize(gz, myObject);
        }

这很好用,只有一个问题:用户可以.gz使用 7Zip 打开文件(在设置文件关联之后),但是他不能只是双击.gz文件中显示的 xml,因为它没有.xml扩展名(虽然内容实际上是存在的,xml 格式和所有)。

问题是:“如何将 XML 序列化为 aGZipStream以便.xml扩展名与文件一起保存在.gz存档中?” 我正在使用.NET 4.0。

谢谢阅读。

4

1 回答 1

0

I have figure out a simple way to solve that. If this way should be considered a hack or a pragmatic and fine solution, is up to each one, I think.

Simply set the GZip filename to myFileName.xml.gz. This actually makes the inner file appear as myFileName.xml (trimming out the .gz extension as before).

I hope this won't break in the future...

于 2013-10-07T19:39:06.013 回答