2013-12-11 58 views
1

我上的应用程序工作的一个zip文件,该文件会从我的服务器上下载一个.zip文件,解压缩下载时完全使用DotNetZip 1.9无法读取,由于使用DotNetZip 1.9

的应用程序下载的zip文件正确,但是当它到达读取.zip文件的部分时,它会抛出异常“无法将其读取为zip文件”(在http://pastebin.com/NTrtMgR9处为完全例外)。

什么抛出我送行,甚至寿它抛出一个异常,该文件仍然正常解压...

这里是我的源:

private void btnDownload_Click(object sender, EventArgs e) 
{ 
    if (!Directory.Exists(HardCorpsPath)) 
    { 
     //MessageBox.Show("Downloading HardCorps Mod Pack (Full)", "Downloading", MessageBoxButtons.OK, MessageBoxIcon.Information); 
     zipFileName = "HardCorps"; 

     HCDownloadURL = String.Format("http://www.survivaloperations.net/client/hardcorps/{0}.zip", zipFileName); 
     HCZipPath = Path.Combine(Arma2OAPath, @"HardCorps.zip"); 

     WebClient Download_Client = new WebClient();//Declaring the webclient as Download_Client 
     Download_Client.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);//the event handler 
     Download_Client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);// " " 
     Download_Client.DownloadFileAsync(new Uri(HCDownloadURL.Trim().ToString()), HCZipPath);// " " 

     //extract 
     using (var zipFile = ZipFile.Read(HCZipPath)) 
     { 
      zipFile.ExtractAll(Arma2OAPath, ExtractExistingFileAction.OverwriteSilently); 
     } 
    } 
    else 
    { 
     MessageBox.Show("Directory Validated!"); 
     //Read users HardCorpsPath\version.txt and compare to server version.txt 
     //if server version > user version, download patch.zip where "patch" == the number version of the server's version.txt (ex: 1001.zip) 
     //On download complete, extract to HardCorpsPath and overwrite silently 
    } 
}//close Download Button 

和我ProgressChanged /完成方法:

private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e) 
{ 
    pbDownloader.Value = e.ProgressPercentage;//setting the progressbar value as downloadprogress 
} 

private void Completed(object sender, AsyncCompletedEventArgs e) 
{ 
    using (var zipFile = ZipFile.Read(String.Format(HCZipPath))) 
    { 
     zipFile.ExtractAll(Arma2OAPath, ExtractExistingFileAction.OverwriteSilently); 
    } 

    MessageBox.Show("Downloading Successful ", "Download_Completed", MessageBoxButtons.OK, MessageBoxIcon.Information);//just a messagebox   
    pbDownloader.Value = (0);//resetting the progressbar 
} 

HCZipPath是一个字符串变量(如果有什么差别)

回答

2

在您的btnDownload_Click方法中,您正试图在文件下载之前解压缩文件。您可以拨打Download_Client.DownloadFileAsync开始下载,并且您已经正确设置DownloadFileCompleted事件的处理程序,在此处尝试再次进行提取。取出DownloadFileAsync方法调用下方的代码(以防止发生异常)。您正在提取文件,因为您在Completed方法中提取它(这是执行此操作的正确方法)。

+1

哦,男孩,我觉得笨拙......当我将该代码移动到Completed()方法时,我以为我已经将它从btnDownload_Click事件中移除了......这只是表明当你看着墙的代码太长,你开始错过简单的事情,感谢新鲜的眼睛! – Meta

+0

@梅塔没问题,我知道这种感觉! – flipchart