How do you download and extract a gzipped file wit

2019-03-12 02:08发布

I need to periodically download, extract and save the contents of http://data.dot.state.mn.us/dds/det_sample.xml.gz to disk. Anyone have experience downloading gzipped files with C#?

标签: c# .net gzip
5条回答
Lonely孤独者°
2楼-- · 2019-03-12 02:47

Try the SharpZipLib, a C# based library for compressing and uncompressing files using gzip/zip.

Sample usage can be found on this blog post:

using ICSharpCode.SharpZipLib.Zip;

FastZip fz = new FastZip();       
fz.ExtractZip(zipFile, targetDirectory,"");
查看更多
做个烂人
3楼-- · 2019-03-12 02:48

The GZipStream class might be what you want.

查看更多
相关推荐>>
4楼-- · 2019-03-12 02:50

To compress:

using (FileStream fStream = new FileStream(@"C:\test.docx.gzip", 
FileMode.Create, FileAccess.Write)) {
    using (GZipStream zipStream = new GZipStream(fStream, 
    CompressionMode.Compress)) {
        byte[] inputfile = File.ReadAllBytes(@"c:\test.docx");
        zipStream.Write(inputfile, 0, inputfile.Length);
    }
}

To Decompress:

using (FileStream fInStream = new FileStream(@"c:\test.docx.gz", 
FileMode.Open, FileAccess.Read)) {
    using (GZipStream zipStream = new GZipStream(fInStream, CompressionMode.Decompress)) {   
        using (FileStream fOutStream = new FileStream(@"c:\test1.docx", 
        FileMode.Create, FileAccess.Write)) {
            byte[] tempBytes = new byte[4096];
            int i;
            while ((i = zipStream.Read(tempBytes, 0, tempBytes.Length)) != 0) {
                fOutStream.Write(tempBytes, 0, i);
            }
        }
    }
}

Taken from a post I wrote last year that shows how to decompress a gzip file using C# and the built-in GZipStream class. http://blogs.msdn.com/miah/archive/2007/09/05/zipping-files.aspx

As for downloading it, you can use the standard WebRequest or WebClient classes in .NET.

查看更多
唯我独甜
5楼-- · 2019-03-12 02:53

Just use the HttpWebRequest class in the System.Net namespace to request the file and download it. Then use GZipStream class in the System.IO.Compression namespace to extract the contents to the location you specify. They provide examples.

查看更多
相关推荐>>
6楼-- · 2019-03-12 03:08

You can use WebClient in System.Net to download:

WebClient Client = new WebClient ();
Client.DownloadFile("http://data.dot.state.mn.us/dds/det_sample.xml.gz", " C:\mygzipfile.gz");

then use #ziplib to extract

Edit: or GZipStream... forgot about that one

查看更多
登录 后发表回答