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#?
我需要定期下载、提取和保存http://data.dot.state.mn.us/dds/det_sample.xml的内容。广州到磁盘。有谁有使用c#下载gzip文件的经验?
5 个解决方案
#1
27
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 WebRequest or WebClient classes in .NET.
至于下载它,您可以使用。net中的标准WebRequest或WebClient类。
#2
7
You can use WebClient in System.Net to download:
您可以在系统中使用WebClient。网下载:
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
然后使用#ziplib提取
Edit: or GZipStream... forgot about that one
编辑:或GZipStream……忘了那一个
#3
4
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.
只需在系统中使用HttpWebRequest类。Net命名空间来请求文件并下载它。然后在System.IO中使用GZipStream类。压缩命名空间,将内容提取到指定的位置。他们提供的例子。
#4
4
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,"");
#1
27
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 WebRequest or WebClient classes in .NET.
至于下载它,您可以使用。net中的标准WebRequest或WebClient类。
#2
7
You can use WebClient in System.Net to download:
您可以在系统中使用WebClient。网下载:
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
然后使用#ziplib提取
Edit: or GZipStream... forgot about that one
编辑:或GZipStream……忘了那一个
#3
4
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.
只需在系统中使用HttpWebRequest类。Net命名空间来请求文件并下载它。然后在System.IO中使用GZipStream类。压缩命名空间,将内容提取到指定的位置。他们提供的例子。
#4
4
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,"");