你如何下载并解压一个压缩文件,用C#? [英] How do you download and extract a gzipped file with C#?

查看:207
本文介绍了你如何下载并解压一个压缩文件,用C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要定期下载,提取和保存的<一个内容href="http://data.dot.state.mn.us/dds/det_sample.xml.gz">http://data.dot.state.mn.us/dds/det_sample.xml.gz到磁盘。任何人都有经验下载gzip文件用C#?

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#?

推荐答案

要融为一体preSS:

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);
  }
}

要DECOM preSS:

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);
      }
    }
  }
}

这是一个职位,我去年写,说明如何DECOM preSS一个gzip文件使用C#和内置的GZipStream类两者。 http://blogs.msdn.com/miah/archive /2007/09/05/zipping-files.aspx

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

至于下载它,你可以使用标准的的WebRequest Web客户端类.NET。

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

这篇关于你如何下载并解压一个压缩文件,用C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆