Search code examples
c#visual-studio-2017

Extracting .zip file into folder C#


I have problem when I try to create code for extracting .zip file into a folder, before I show you code, I want to tell you what I need to do?

Its simple, I want to write code so that when a user clicks on a button, it deletes a directory, and then downloads a new .zip file and extracts it at the same directory and name which was deleted... Its something like restoring directory to default form..

I successfully wrote code for deleting the directory and downloading .zip file but I cant write code for extracting that .zip ...

Here is the code

private void button2_Click(object sender, EventArgs e)
{
    // Is file downloading yet?
    if (webClient != null)
        return;

    var sprdir = new DirectoryInfo(@"cstrike/sprites");
    string sprzippath = @"cstrike/sprites.zip";
    string extzippath = @"cstrike";
    if (!sprdir.Exists)
    {
        webClient = new WebClient();
        webClient.DownloadFileAsync(new Uri("https://sipi-portfolio.000webhostapp.com/csfiledownload/sprites.zip"), @"cstrike/sprites.zip");
    }
    else
    {
        sprdir.Attributes = sprdir.Attributes & ~FileAttributes.ReadOnly;
        sprdir.Delete(true);
        webClient = new WebClient();
        webClient.DownloadFileAsync(new Uri("https://sipi-portfolio.000webhostapp.com/csfiledownload/sprites.zip"), @"cstrike/sprites.zip");
    }

}

And yea, I tried with using System.IO and System.IO.Compress and ZipFile.ExtractToDirectory and ExtractToDirectory, no one working... Just get red line below the text..


Solution

  • So first you need to add assembly System.IO.Compression.FileSystem into your projects.

    Second thing is that you are using DownloadFileAsync which probably was not yet completed so your extraction fails (because no file exists yet)

    The 3rd is that you are not creating the folder if it does not exists and that makes WebClient.DownloadFileAsync fail.

    you need to register to the event where DownlodFileCompleted and to the extraction there.

    here is an example:

    using System;
    using System.ComponentModel;
    using System.IO;
    using System.Net;
    using System.Windows.Forms;
    using System.IO.Compression;
    
    namespace Stack
    {
        public partial class Form1 : Form
        {
            WebClient webClient;// = new WebClient();
            const string basPath = @"D:\test";
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                // Is file downloading yet?
                if (webClient != null)
                    return;
    
                //var sprdir = new DirectoryInfo(@"cstrike/sprites");
    
                var sprdir = new DirectoryInfo(basPath);
                string sprzippath = $"{basPath}/sprites.zip";
                string extzippath = @"cstrike";
                if (!sprdir.Exists)
                {
                    Directory.CreateDirectory(basPath);
                    webClient = new WebClient();
                    webClient.DownloadFileCompleted += ExtratcZip;
                    webClient.DownloadFileAsync(
                        new Uri("https://sipi-portfolio.000webhostapp.com/csfiledownload/sprites.zip"),
                        $"{basPath}/sprites.zip");
                }
                else
                {
                    sprdir.Attributes = sprdir.Attributes & ~FileAttributes.ReadOnly;
                    sprdir.Delete(true);
                    Directory.CreateDirectory(basPath);
                    webClient = new WebClient();
                    webClient.DownloadFileCompleted += ExtratcZip;
                    webClient.DownloadFileAsync(
                        new Uri("https://sipi-portfolio.000webhostapp.com/csfiledownload/sprites.zip"),
                        $"{basPath}/sprites.zip");
                }
            }
    
            private void ExtratcZip(object sender, AsyncCompletedEventArgs e)
            {
                ZipFile.ExtractToDirectory($"{basPath}/sprites.zip", $"{basPath}");
            }
        }
    }
    

    hope it helps.