通过 URL 下载时 zip 文件损坏

发布于 2024-12-18 01:13:36 字数 2192 浏览 3 评论 0原文

我从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏日浅笑〃 2024-12-25 01:13:36

如何使用 WebClient.DownloadFile 方法 (WebClient.DownloadFile)

using (var wc = new WebClient())
{
  wc.DownloadFile(Url, ResultFileName);
}

How about using the WebClient.DownloadFile method (WebClient.DownloadFile)

using (var wc = new WebClient())
{
  wc.DownloadFile(Url, ResultFileName);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文