1

I'm downloading a zip file from a URL and when trying to extract it manually just to check it has come through correctly it shows it's empty and doesn't let me.

try {
     using (var client = new WebClient())
     {
         client.DownloadFile("url", "C:/1.zip");
     }
} catch(Exception e) {
     Debug.WriteLine(e + "DDDD");
}

Also how would I programmatically extract this so I can go into the contents of the file and extract more things. What is the simplest way?

Suneel Kumar
  • 1,650
  • 2
  • 21
  • 31
Sam
  • 1,207
  • 4
  • 26
  • 50
  • Where are you downloading from? A public resource or a custom application of your own design? – DiskJunky Oct 11 '17 at 10:59
  • 1
    @DiskJunky it's a public resource but I cannot share the URL i'm afraid as it's internal use only. – Sam Oct 11 '17 at 11:01
  • For the unzip part : https://stackoverflow.com/questions/836736/unzip-files-programmatically-in-net?rq=1 – Goufalite Oct 11 '17 at 11:10

3 Answers3

0

Try using the Async method and the events that go with it. Something like this:

void Foo() {
    var webClient = new WebClient();
    var totalBytes = 0l;
    var destFile = new FileInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "platform-tools-latest-windows.zip"));
    webClient.DownloadProgressChanged += (s, e) => Debug.WriteLine($"Download progress changed: { e.ProgressPercentage }% ({ e.BytesReceived } / { (totalBytes = e.TotalBytesToReceive) })");
    webClient.DownloadFileCompleted += (s, e) => {
        destFile.Refresh();
        if (destFile.Length != totalBytes) {
            // Handle error
        } else {
            // Do nothing?
        }
    };
    webClient.DownloadFileAsync(new Uri("https://dl.google.com/android/repository/platform-tools-latest-windows.zip"), destFile.FullName);

}

Give that a try and see if it works with your zip

EDIT: If the above code doesn't work, there are a few more possibilities worth trying.

1: Try appending while (webClient.IsBusy); to the end of the above method, to force the running thread to wait until the WebClient has finished downloading

2: Try downloading the raw data (byte[]) first, then flushing the buffer to the file.

NOTE: ONLY DO THIS FOR SMALL(er) FILES!

    public void DownloadFoo() {
        var webClient = new WebClient();
        var totalBytes = 0l;
        var destFile = new FileInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "platform-tools-latest-windows.zip"));
        webClient.DownloadProgressChanged += (s, e) => Debug.WriteLine($"Download progress changed: { e.ProgressPercentage }% ({ e.BytesReceived } / { (totalBytes = e.TotalBytesToReceive) })");

        using (webClient) {
            var buffer = webClient.DownloadData(new Uri("https://dl.google.com/android/repository/platform-tools-latest-windows.zip"));

            using (var oStream = destFile.Open(FileMode.Truncate)) {
                oStream.Write(buffer, 0, buffer.Length);
                oStream.Flush(true); // true => flushToDisk
            }
        }

        // webClient is automatically disposed of; method will return cleanly

    }
SimonC
  • 1,547
  • 1
  • 19
  • 43
  • It still cannot be opened manually it shows as empty. Is this because it needs to be extracted programmitcally? – Sam Oct 11 '17 at 11:41
  • The above snippet contains a valid link; have you tried executing the code above - as it is - and opening the resulting file? Were you able to open the file? – SimonC Oct 11 '17 at 11:42
  • Just tried it, using this url https://dl.google.com/android/repository/platform-tools-latest-windows.zip still does not allow me to open it – Sam Oct 11 '17 at 11:44
  • What application are you using to open the zip? What are its properties? Is the zip file valid? – SimonC Oct 11 '17 at 11:45
  • Tried to right click and Extract all it shows as empty cannot open and 7Zip shows can not open file as archive – Sam Oct 11 '17 at 11:47
  • Can you upload the file somewhere and provide a link? I'd like to see that file. – SimonC Oct 11 '17 at 11:48
  • File is emtpy not letting me upload it to onedrive I will try somewhere else – Sam Oct 11 '17 at 11:53
  • Another question, is what is the surrounding code? Is your application a console app? If so, it might be possible your application terminates before the download completes. See my edit for more information – SimonC Oct 11 '17 at 11:57
  • This is still not working properly. I have done some digging into the code when trying to unzip and I get wrong local header signature 0x88B1F. Any ideas? It could be I need to retrieve it with certain headers? – Sam Oct 12 '17 at 12:26
  • Doubtful, that's regarding headers in the file itself. If the file size is < 1kb there's a good chance there are no headers at all - the file is completely empty. – SimonC Oct 13 '17 at 11:03
  • yep fixed it problem is it works as a rest call passing arguments through the URL needed to use REST Sharp . Thanks for your help, much appreciated – Sam Oct 13 '17 at 13:36
0

You can extract you zip file using below code.

System.IO.Compression.ZipFile.ExtractToDirectory(@"C:/1.zip",@"c:\example\extract");

Do not forget to add System.IO.Compression.FileSystem from assembly.

Govinda Rajbhar
  • 2,926
  • 6
  • 37
  • 62
0

Checked the URL through HTTP Headers in Firefox. Found this URL was going to some sort of API before giving the zip file. The URL had parameters passed through as arguments.

I installed RestSharp then done this:

var client = new RestClient(URL);
var request = new RestRequest("&amp;user=bam&amp;pass=boom", Method.GET);
var queryResult = client.Execute(request);
string zipPath = C:/Temp + zippy.zip";
client.DownloadData(request).SaveAs(zipPath);
Sam
  • 1,207
  • 4
  • 26
  • 50