下载zip文件与webclient似乎不能正常工作,一旦下载并保存压缩文件出现无效或损坏,用zip压缩阅读器打开。然而,源zip文件似乎很好,它是一个有效的zip。c#WebClient下载zip破坏它
下载代码:
using (WebClient webClient = new WebClient())
{
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
webClient.DownloadFileAsync(new Uri(URL), downloadZipFilename);
}
private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
progressBar.Value = e.ProgressPercentage;
}
private void Completed(object sender, AsyncCompletedEventArgs e)
{
//unzip
using (ZipFile zipFile = ZipFile.Read(currentTemporaryDownloadFileUrl))
{
zipFile.ExtractAll(currentTargetFileUrl);
}
File.Delete(currentTemporaryDownloadFileUrl);
DownloadFinished(this,EventArgs.Empty);
Console.WriteLine("File finished downloading.");
}
拉链提取出来作为腐败。
Server代码:
//send file
e.Response.Connection.Type = HttpServer.Headers.ConnectionType.Close;
byte[] buffer = ReadFile(filePath);
e.Response.Body.Write(buffer, 0, buffer.Length);
READFILE服务器上:
public static byte[] ReadFile(string filePath)
{
// this method is limited to 2^32 byte files (4.2 GB)
FileStream fs = File.OpenRead(filePath);
try
{
byte[] bytes = new byte[fs.Length];
fs.Read(bytes, 0, Convert.ToInt32(fs.Length));
fs.Close();
return bytes;
}
finally
{
fs.Close();
}
}
这里有什么错?
感谢, 基督教斯图尔特
您可能在'ReadFile'中耗尽内存。 –
您是否将下载的文件与服务器上的文件进行了比较? –
任何想法?任何人 –