通过 URL 下载时 zip 文件损坏
我从 http://download.geonames.org/export/ 下载 3(2 zip 1 txt 文件) 文件dump/ 使用 WebClient 和大小为 9 Mb 的 zip 文件之一,当我下载它时,它的大小为 215 Mb 并且已损坏..我尝试使用 WebRequest 和 FileStream 类,但再次得到相同的结果..
我的备用 WebClient 方法下载:
private void MyDownloadFile(Uri url, string outputFilePath)
{
const int BUFFER_SIZE = 16 * 1024;
using (var outputFileStream = File.Create(outputFilePath, BUFFER_SIZE))
{
var req = WebRequest.Create(url);
using (var response = req.GetResponse())
{
using (var responseStream = response.GetResponseStream())
{
var buffer = new byte[BUFFER_SIZE];
int bytesRead;
do
{
bytesRead = responseStream.Read(buffer, 0, BUFFER_SIZE);
outputFileStream.Write(buffer, 0, bytesRead);
} while (bytesRead > 0);
}
}
}
}
private void DownloadFile(String Url, String ResultFileName)
{
HttpWebRequest wr = (HttpWebRequest)HttpWebRequest.Create(Url);
HttpWebResponse ws = (HttpWebResponse)wr.GetResponse();
Stream str = ws.GetResponseStream();
byte[] inBuf = new byte[100000];
int bytesReadTotal = 0;
FileStream fstr = new FileStream(ResultFileName, FileMode.Create, FileAccess.Write);
while (true)
{
int n = str.Read(inBuf, 0, 100000);
if ((n == 0) || (n == -1))
{
break;
}
fstr.Write(inBuf, 0, n);
bytesReadTotal += n;
}
str.Close();
fstr.Close();
}
下载时损坏的文件 URL: http://download.geonames.org/export/ dump/allCountries.zip
有人有同样的问题或者可以编写正确上传此 zip 文件的方法吗?或者也许我做错了什么?
I download 3(2 zip 1 txt file) files from http://download.geonames.org/export/dump/ use WebClient and one of zip file which have size 9 Mb, when i download it have size 215 Mb and corrupt..I try use WebRequest and FileStream class but again have same result..
My methods for alternate WebClient download :
private void MyDownloadFile(Uri url, string outputFilePath)
{
const int BUFFER_SIZE = 16 * 1024;
using (var outputFileStream = File.Create(outputFilePath, BUFFER_SIZE))
{
var req = WebRequest.Create(url);
using (var response = req.GetResponse())
{
using (var responseStream = response.GetResponseStream())
{
var buffer = new byte[BUFFER_SIZE];
int bytesRead;
do
{
bytesRead = responseStream.Read(buffer, 0, BUFFER_SIZE);
outputFileStream.Write(buffer, 0, bytesRead);
} while (bytesRead > 0);
}
}
}
}
private void DownloadFile(String Url, String ResultFileName)
{
HttpWebRequest wr = (HttpWebRequest)HttpWebRequest.Create(Url);
HttpWebResponse ws = (HttpWebResponse)wr.GetResponse();
Stream str = ws.GetResponseStream();
byte[] inBuf = new byte[100000];
int bytesReadTotal = 0;
FileStream fstr = new FileStream(ResultFileName, FileMode.Create, FileAccess.Write);
while (true)
{
int n = str.Read(inBuf, 0, 100000);
if ((n == 0) || (n == -1))
{
break;
}
fstr.Write(inBuf, 0, n);
bytesReadTotal += n;
}
str.Close();
fstr.Close();
}
File URL which corrupt while download: http://download.geonames.org/export/dump/allCountries.zip
Anyone, have same problem or can write method for correct upload this zip file?? Or maybe i do something wrong??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如何使用 WebClient.DownloadFile 方法 (WebClient.DownloadFile)
How about using the WebClient.DownloadFile method (WebClient.DownloadFile)