如何使用 C# 下载和解压缩 gzip 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-01 08:56:48  来源:igfitidea点击:

How do you download and extract a gzipped file with C#?

提问by John Sheehan

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

我需要定期下载、提取http://data.dot.state.mn.us/dds/det_sample.xml.gz的内容并将其保存到磁盘。任何人都有使用 C# 下载 gzip 文件的经验?

采纳答案by JeremiahClark

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

摘自我去年写的一篇文章,该文章展示了如何使用 C# 和内置的 GZipStream 类解压缩 gzip 文件。 http://blogs.msdn.com/miah/archive/2007/09/05/zipping-files.aspx

As for downloading it, you can use the standard WebRequestor WebClientclasses in .NET.

至于下载,您可以使用.NET 中的标准WebRequestWebClient类。

回答by Patrick

The GZipStreamclass might be what you want.

GZipStream类可能是你想要的。

回答by Dale Ragan

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

只需使用System.Net 命名空间中的HttpWebRequest类来请求文件并下载它。然后使用System.IO.Compression 命名空间中的GZipStream类将内容提取到您指定的位置。他们提供了例子。

回答by Adam Haile

You can use WebClient in System.Net to download:

您可以在 System.Net 中使用 WebClient 进行下载:

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

then use #ziplibto extract

然后使用#ziplib提取

Edit: or GZipStream... forgot about that one

编辑:或 GZipStream ......忘记了那个

回答by Yaakov Ellis

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

试试SharpZipLib,这是一个基于 C# 的库,用于使用 gzip/zip 压缩和解压缩文件。

Sample usage can be found on this blog post:

可以在此博客文章中找到示例用法:

using ICSharpCode.SharpZipLib.Zip;

FastZip fz = new FastZip();       
fz.ExtractZip(zipFile, targetDirectory,"");