I am working on an application that will download a .zip file from my server and extract it when the download is complete using DotNetZip 1.9

The application is downloading the zip file properly, but when it gets to the section to read the .zip file it throws the exception "Cannot read that as a zip file" (full exception at http://pastebin.com/NTrtMgR9).

What throws me off, is even tho it is throwing an exception, the file is still unzipped properly...

Here is my source:

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

and my ProgressChanged/Completed methods:

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 is a string variable (if that makes any difference)

有帮助吗?

解决方案

In your btnDownload_Click method, you are trying to extract the file before it has been downloaded. You call Download_Client.DownloadFileAsync which starts the download, and you've correctly set up the handler for the DownloadFileCompleted event where you try to do the extraction (again). Take out the code below the DownloadFileAsync method call (to prevent the exception). You file is being extracted because you extract it in the Completed method (which is the correct way to do it).

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top