简体   繁体   中英

c# WebClient Downloading a Zip Corrupts it

Downloading Zip files with webclient seems to not be working properly for me, once downloaded and saved the zip file comes up as invalid or corrupted, opening with a zip reader. However the source zip file seems to be fine, its a valid zip.

Download code:

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.");
    }

The zip extract comes up as corrupt.

Server code:

 //send file
                    e.Response.Connection.Type = HttpServer.Headers.ConnectionType.Close;
                    byte[] buffer = ReadFile(filePath);
                    e.Response.Body.Write(buffer, 0, buffer.Length);

Readfile on server:

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();
            }
        }

What's wrong here?

Thanks, Christian Stewart

In the WebClient call you use downloadZipFilename while int the Completed method you use currentTemporaryDownloadFileUrl ... could it be that the Completed method tries to unzip some other file and the downloaded one ?

Your server code supports only <= 2 GB (Int32 is an alias for int which is signed!)... is the file perhaps bigger than 2 GB ? If the file is bigger then your server codes sends for the "rest" (above 2 GB) all bytes as 0x00 which definitely makes the ZIP corrupt...

Rather than, inefficiently, loading the ZIP file into memory, then writing it out, how about using

e.Response.TransmitFile(filePath);

TransmitFile streams directly to the output stream without buffering, thus minimising memory consumption.

I suggest that you de-couple the server transmission from the client read-and-unpack. that way you can isolate any problems that arise.

Make sure the server "create and transmit zip file" thing works properly. Try hitting the server from a browser, make sure you can unpack the zip file successfully. Then add in the C# client, to download-and-unpack the zip file programmatically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM