如何使用C#下载并解压缩gzip文件?
我需要定期下载,提取和保存的内容到磁盘上。任何人都有使用C#?下载gzip文件的经验。该类可能是您想要的。只需使用System.Net命名空间中的类来请求并下载该文件即可。然后使用System.IO.Compression命名空间中的类将内容提取到指定的位置。它们提供了示例。您可以使用System.Net中的WebClient下载:如何使用C#下载并解压缩gzip文件?,c#,.net,gzip,C#,.net,Gzip,我需要定期下载,提取和保存的内容到磁盘上。任何人都有使用C#?下载gzip文件的经验。该类可能是您想要的。只需使用System.Net命名空间中的类来请求并下载该文件即可。然后使用System.IO.Compression命名空间中的类将内容提取到指定的位置。它们提供了示例。您可以使用System.Net中的WebClient下载: WebClient Client = new WebClient (); Client.DownloadFile("http://data.dot.state.mn
WebClient Client = new WebClient ();
Client.DownloadFile("http://data.dot.state.mn.us/dds/det_sample.xml.gz", " C:\mygzipfile.gz");
然后使用提取
编辑:或GZipStream。。。忘了那一个吧试试,这是一个基于C#的库,用于使用gzip/zip压缩和解压文件
可以在以下网站上找到示例用法:
压缩:
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);
}
}
要解压缩:
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);
}
}
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);
}
}
}
}
摘自我去年写的一篇文章,该文章展示了如何使用C#和内置的GZipStream类解压缩gzip文件。
至于下载,您可以使用.NET中的标准或类 +1该链接对我刚才第一次使用压缩很有帮助。漂亮、有用、简洁的博客文章。