how to extract zip files in c# to a folder where the system is installed

How to extract zip files in c # to a folder where the system is installed?

using System;
using System.IO.Compression;
using System.Windows;
using System.Xml;

namespace TestXml
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string Dir2 = @"c:\IASD\Cantina Escolar\";
            XmlDocument doc = new XmlDocument();
            doc.Load("http://www.meusite.com/arquivoXML.xml");

            XmlNode node = doc.DocumentElement.SelectSingleNode("/Application/Version");
            XmlNode node1 = doc.DocumentElement.SelectSingleNode("/Application/ZipFile");
            string version = node.InnerText;
            string zipfile = node1.InnerText;

            string End = "http://www.meusite.com/";

            string Arq = version;
            string file = zipfile;

            string Arquivo = String.Concat(End, zipfile);
            string Arquivo2 = String.Concat(@"c:\IASD\Cantina Escolar\",zipfile); 

            WebClient webClient = new WebClient();
            webClient.DownloadFile(Arquivo, @"C:\IASD\Cantina Escolar\"+zipfile);

            ZipFile.ExtractToDirectory(Arquivo2, zipfile);
        }
    }
}

The error message displayed is:

URI formats are not supported.

Then summarizing:

I go on the server and put a zip, which is an update to be downloaded. Manually change the XML file putting the version of the file, which is nothing more than the name of the file to be unpacked. The method reads the XML and has to download and unzip the zip file into the system installation folder.

Could you help me?

Author: Maniero, 2014-03-27

2 answers

This is because you are waiting for the Unzip function to be able to download your zip file, and that's not how it works. First you need to download the file, save it somewhere and then open it and unzip it.

The following code should work:

using System;
using System.IO.Compression;
using System.Windows;
using System.Xml;

namespace TestXml
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var dir = "c:\\pasta\\Diretório_onde_descompactar";

            //abrindo e lendo um arquivo xml para encontrar a versão que está disponível
            XmlDocument doc = new XmlDocument();
            doc.Load("http://www.meusite.com/pasta/arquivoXML.xml");
            XmlNode node = doc.DocumentElement.SelectSingleNode("/Application/Version");
            var version = node.InnerText;

            //Aqui eu pego o endereço onde é para descompactar
            // e informo para o ZipFile.ExtractTodirectory
            //passando a concatenação como parâmentro
            var url = "http://meusite.com/pasta/";

            var arq = version;

            var urlArquivo = String.Concat(url, arq, ".zip");

            // Download
            var webRequest = (HttpWebRequest)WebRequest.Create(urlArquivo);            
            var response = webRequest.GetResponse() as HttpWebResponse;
            var stream = response.GetResponseStream();

            using (ZipInputStream zipStream = new ZipInputStream(stream))
            {
                ZipEntry currentEntry;
                while ((currentEntry = zipStream.GetNextEntry()) != null)
                {
                    currentEntry.Extract(dir, ExtractExistingFileAction.OverwriteSilently);
                }
            }
        }
    }
}
 1
Author: Leonel Sanches da Silva, 2014-03-27 15:34:28

After the code change, the problem that occurs is that the use of DownloadFile is incorrect. String.Concat does not map the directory on the machine: it only mounts a String with a directory name.

The correct one in this case is to use Path.Combine().

The final code looks like this:

using System;
using System.IO.Compression;
using System.IO;
using System.Windows;
using System.Xml;

namespace TestXml
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string Dir2 = @"c:\IASD\Cantina Escolar\";
            XmlDocument doc = new XmlDocument();
            doc.Load("http://www.meusite.com/arquivoXML.xml");

            XmlNode node = doc.DocumentElement.SelectSingleNode("/Application/Version");
            XmlNode node1 = doc.DocumentElement.SelectSingleNode("/Application/ZipFile");
            string version = node.InnerText;
            string zipfile = node1.InnerText;

            string End = "http://www.meusite.com/";

            string Arq = version;
            string file = zipfile;

            string Arquivo = String.Concat(End, zipfile);
            string destino = Path.Combine(@"c:\IASD\Cantina Escolar\" + zipfile);

            WebClient webClient = new WebClient();
            webClient.DownloadFile(Arquivo, destino);

            ZipFile.ExtractToDirectory(@"c:\IASD\Cantina Escolar\", destino);
        }
    }
}
 1
Author: Leonel Sanches da Silva, 2014-03-27 16:52:34