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

1

How to extract zip files in to a folder where is the system 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.

So summarizing:

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

Could you help me?

    
asked by anonymous 27.03.2014 / 14:04

2 answers

1

This is because you are expecting the decompression function to be able to download your zip file, and that is not how it works. First you need to download the file, save 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);
                }
            }
        }
    }
}
    
27.03.2014 / 16:34
1

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 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);
        }
    }
}
    
27.03.2014 / 17:37