-2

So what I'm trying to do - download zip file from URI, and extract it into some directory but I'm getting THIS error:

"System.IO.IOException: "The file 'C:\Users\yup_c\source\repos\LauncherYCFPS\bin\Debug\net5.0-windows\new_ver.zip' already exists."

So I think it caused by wc.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadFinished); because it isn't working properly and static void DownloadFinished(object sender, EventArgs e) launching without waiting file downlod, where am I going wrong?

using System;
using System.Collections.Generic;
using System.IO.Compression;
using System.Data;
using System.Net;
using System.Windows.Forms;
using System.ComponentModel;

namespace LauncherYCFPS
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            {
                using (WebClient wc = new WebClient())
                {
                    wc.DownloadProgressChanged += wc_DownloadProgressChanged;
                    wc.DownloadFileAsync(
                        // Param1 = Link of file
                        new System.Uri("http://o997388w.beget.tech/new_ver.zip"),
                        // Param2 = Path to save
                        @".\new_ver.zip"
                    ) ;
                    wc.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadFinished);
                }
                static void DownloadFinished(object sender, EventArgs e)
                {
                    string startPath = @".\";
                    string zipPath = @".\new_ver.zip";
                    string extractPath = @".\extract";

                    ZipFile.CreateFromDirectory(startPath, zipPath);

                    ZipFile.ExtractToDirectory(zipPath, extractPath);
                }
            }
            // Event to track the progress
            void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
            {
                progressBar1.Value = e.ProgressPercentage;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

mxmissile
  • 11,464
  • 3
  • 53
  • 79
YupCore
  • 25
  • 3
  • Add all event handlers before you start the procedure. Provide a full path instead of these things: `@".\new_ver.zip"` etc. (when you deploy, you'll have no idea where that file will end up). Check if the same file already exists and ask the User if the file should be replaced, duplicate or the operation should be aborted. – Jimi Mar 11 '21 at 20:46
  • `await DownloadFileAsync` or you can use the non-async `DownloadFile`. Although I don't get why you are downloading it to file only to then zip it. You can download through a stream and compress the stream as you go – Charlieface Mar 11 '21 at 20:50
  • Does this answer your question? [How to make an Asynchronous Method return a value?](https://stackoverflow.com/questions/6045343/how-to-make-an-asynchronous-method-return-a-value) See also [Download file directly to memory](https://stackoverflow.com/questions/19686599/download-file-directly-to-memory) – Charlieface Mar 11 '21 at 20:54
  • @Charlieface You cannot await `DownloadFileAsync`. – Jimi Mar 11 '21 at 21:01

2 Answers2

0

Look at the error, there's already a file with that name!

You must either delete the directory, before you use ZipFile.ExtractToDirectory method OR use the method, that will overwrite existing directory:

ZipFile.ExtractToDirectory(zipPath, extractPath, true);
Poul Bak
  • 10,450
  • 5
  • 32
  • 57
0

Use the overload that allows you to overwrite files:

public static void ExtractToDirectory (string sourceArchiveFileName, string destinationDirectoryName, System.Text.Encoding? entryNameEncoding, bool overwriteFiles);

Julius
  • 187
  • 5