开发者

How to download and unzip a sitemap gz file in c#?

开发者 https://www.devze.com 2022-12-08 09:23 出处:网络
I need to download and unzip 开发者_StackOverflowa sitemap.xml file that is compressed (maybe tar + gzip?) into a sitemap.xml.gz

I need to download and unzip 开发者_StackOverflowa sitemap.xml file that is compressed (maybe tar + gzip?) into a sitemap.xml.gz

From Windows I use 7zip. But note that the gz contains a directory with the same name of the zipped file (maybe due to tar + gx)

How can I do in c#?

Thanks


Use the GZip Stream class to unzip the XML document.

Something like:

var file = File.Open("C:\test.xml.gz", FileMode.Open);
var zip = new GZipStream(file, CompressionMode.Decompress);
var doc = new XmlDocument();
doc.Load(zip);

Edit

To be more clean with our IDisposables:

var doc = new XmlDocument();
using(var file = File.Open("C:\test.xml.gz", FileMode.Open))
using(var zip = new GZipStream(file, CompressionMode.Decompress))
{
    doc.Load(zip);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消