我想从某个网址下载ZIP文件。 当我打开浏览器并写入URL时,浏览器直接开始下载ZIP文件。但是我想要的是使用C#代码自动执行此操作。
我尝试过以下代码:
private void btnDownload_Click(object sender, EventArgs e) {
WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
webClient.DownloadFileAsync(new Uri("http://---/file.zip"), @"c:\file.zip");
}
private void Completed(object sender, AsyncCompletedEventArgs e) {
MessageBox.Show("Download completed!");
}
似乎下载工作正常,但当我检查下载的文件时,我发现它是 0 KB。
知道发生了什么事吗?
答案 0 :(得分:3)
正如我所看到的,您的代码对应于已知的反模式Timer and Garbage Collector。
btnDownload_Click
完成后,webClient
变量无法访问,垃圾收集器会将其与其功能一起销毁。
试试这个:
private WebClient webClient = null;
private void btnDownload_Click(object sender, EventArgs e) {
// Is file downloading yet?
if (webClient != null)
return;
webClient = new WebClient();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
webClient.DownloadFileAsync(new Uri("http://---/file.zip"), @"c:\file.zip");
}
private void Completed(object sender, AsyncCompletedEventArgs e) {
webClient = null;
MessageBox.Show("Download completed!");
}
现在`webClient是该类的成员,可以访问。然后垃圾收集器不会破坏它。
答案 1 :(得分:1)
这有效:
WebClient webClient = new WebClient();
webClient.Headers.Add("Accept: text/html, application/xhtml+xml, */*");
webClient.Headers.Add("User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");
webClient.DownloadFileAsync(new Uri("https://www.nseindia.com/content/historical/DERIVATIVES/2016/AUG/fo05AUG2016bhav.csv.zip"),"test1.zip");
答案 2 :(得分:1)
您可以像这样轻松使用ZipFile类:
using System.IO.Compression;
class Program
{
static void Main()
{
// Create a ZIP file from the directory "source".
// ... The "source" folder is in the same directory as this program.
// ... Use optimal compression.
ZipFile.CreateFromDirectory("source", "destination.zip",
CompressionLevel.Optimal, false);
// Extract the directory we just created.
// ... Store the results in a new folder called "destination".
// ... The new folder must not exist.
ZipFile.ExtractToDirectory("destination.zip", "destination");
}
}
请注意,这只是available from .Net Framework version 4.5以后......
答案 3 :(得分:0)
我也有这个问题,但我找到了一个简单的解决方案 我想从“https://tools.ietf.org/html/rfc4514”下载此Zip文件 并提取它们。但webClient不能下载它。
我的解决方案:
第1步:将我的FileName从“D:\ update.zip”更改为“D:\ update.zi”webClient.DownloadFileAsync("http://sodco.ir/Choobkhat/Update/update.zip", "D:\\update.zi);
它将开始下载,下载完成后:
第2步:将update.zi重命名为update.zi p
File.Move("update.zi", "update.zip");
第3步:提取它们
ZipFile.ExtractToDirectory(Application.StartupPath + "\\update.zip", Application.StartupPath);
答案 4 :(得分:-3)
完整示例:Full sample at .Net Fiddle
public static bool DownloadFile(string filenameXML){
HttpWebRequest request;
HttpWebResponse response = null;
FileStream fs = null;
long startpoint = 0;
NewSourceFilePath=filenameXML;
fs = File.Create(NewSourceFilePath);
request = (HttpWebRequest)WebRequest.Create("http://---/file.zip");
request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version11;
request.Method = "GET";
request.ContentType = "gzip";
request.Timeout=10000;
request.Headers.Add("xRange", "bytes " + startpoint + "-");
response = (HttpWebResponse)request.GetResponse();
Stream streamResponse = response.GetResponseStream();
byte[] buffer = new byte[1024];
int read;
while ((read = streamResponse.Read(buffer, 0, buffer.Length)) > 0){
fs.Write(buffer, 0, read);}
fs.Flush();fs.Close();
}