0

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..

bodangly
  • 2,473
  • 17
  • 28
  • 1
    Does this answer your question? [How to extract ZIP file in C#](https://stackoverflow.com/questions/4296918/how-to-extract-zip-file-in-c-sharp) – gunr2171 Apr 03 '20 at 14:48
  • 1
    *i tried with using System.IO and System.IO.Compress and ZipFile.ExtractToDirectory*, where is that in your code? – RoadRunner Apr 03 '20 at 14:54
  • Im tried that what you send, and i deleted that codes bcs not working .. –  Apr 03 '20 at 16:58

1 Answers1

0

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.

  • i'll try that and i will message you, thanks you anyway :) –  Apr 03 '20 at 16:59
  • Again same error ... https://prnt.sc/rsg4jh , https://prnt.sc/rsg50x –  Apr 03 '20 at 17:12
  • You need to add the assembly to your project. – Aviad Hasidof Apr 03 '20 at 17:29
  • Ah, i forgot, thanks, now no red line, ill go test now :) TY <3 –  Apr 03 '20 at 17:36
  • Ah, not extract sprites.zip anywhere, but downloaded it and when i try to open extract sprites.zip they say me the file is corrupted .. ? –  Apr 03 '20 at 17:41
  • Can you help me please ? –  Apr 04 '20 at 08:54
  • Can you share the error that you got? For me the code that i sent you works. – Aviad Hasidof Apr 04 '20 at 16:20
  • No error, just not working.. its download .zip file but not extract it, when i try extract it i got this message "Error: File iz corrupted" im using WinRar... –  Apr 04 '20 at 17:22
  • maybe your file is indeed corrupted if winrar cannot unzip it. what file are you trying to unzip? – Aviad Hasidof Apr 04 '20 at 17:24
  • sprites.zip, in that zip i have folder sprites in folder sprites i have files with extension .spr and one folder what's called effects and in that folder i have .spr files too :) that files and folders not corrupted bcs that files are from Counter-Strike 1.6 Game .. –  Apr 04 '20 at 17:49
  • Is that the zip from here: https://sipi-portfolio.000webhostapp.com/csfiledownload/sprites.zip It works good for me with winrar as well. – Aviad Hasidof Apr 04 '20 at 17:58
  • Yup that is that –  Apr 04 '20 at 18:01
  • When you download it normally (not c# code) and try to extract it with winrar or windows does it work for u? – Aviad Hasidof Apr 04 '20 at 18:06
  • Just a moment to open project –  Apr 04 '20 at 18:09
  • 1
    I think the problem is that you are not creating the sprites directory and the DownloadFileAsync does not create it automatically for you. please make sure the destination folder exists before you download. – Aviad Hasidof Apr 04 '20 at 18:14
  • Yeaaaaa, that is the problem, i tried now with empty sprites folder and it downloaded and extracted it, thanks you ... –  Apr 04 '20 at 18:17