tags:

views:

331

answers:

1

I need to download and unzip a 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

+2  A: 

Use the GZip Stream class to unzip the XML document.

Something like:

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

Edit

To be more clean with our IDisposables:

XmlDocument doc = new XmlDocument();
using(FileStream file = File.Open("C:\test.xml.gz", FileMode.Open))
{
    using(GZipStream zip = new GZipStream(file, CompressionMode.Decompress))
    {
        doc.Load(zip);
    }
}
John Gietzen
I would wrap the streams in using statements, but otherwise spot on.
Joel Mueller
Good point, but I was trying to be concise.
John Gietzen