Загрузка Zip-файлов с помощью веб-клиента, похоже, не работает должным образом для меня, после загрузки и сохранения zip-файл отображается как недействительный или поврежденный и открывается с помощью zip-ридера. Однако с исходным zip-файлом все в порядке, это правильный 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.");
}
Извлечение zip оказывается поврежденным.
Код сервера:
//send file
e.Response.Connection.Type = HttpServer.Headers.ConnectionType.Close;
byte[] buffer = ReadFile(filePath);
e.Response.Body.Write(buffer, 0, buffer.Length);
Чтение файла на сервере:
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
. - person Daniel A. White   schedule 25.07.2011